0

Thanks in advance for looking @ this question:

I'm attempting to parse a CSV file, and return the values of certain fields. The code runs perfectly when inline as a script, but when I try to create a function to be accessible by other modules w/in my app, I run into an problem with the variables (keys/values) that I'm pulling from the csv file.

Code to parse CSV (parseCsv.py):

groups =[]
enterprise = []
workinglist = []

def parse_csv(filename):
  quote_char = '"'
  delimiter = ','
  csv_to_parse = open(filename, 'rb')
  csv_reader = csv.DictReader(csv_to_parse, fieldnames=[], restkey='undefined-fieldnames', delimiter=delimiter, quotechar=quote_char)
  current_row = 0
  for row in csv_reader:
    current_row +=1
    if current_row == 1:
      csv_reader.fieldnames = row['undefined-fieldnames']
      continue
    groups.append(row['groupid'])
    enterprise.append(row['enterprise'])
    workinglist.append(row)

I've tried using the global parameter for groups, enterprise, and workinglist; and also tried adding the return groups, return workinglist, return enterprise

Regardless of the syntax, I cannot access the groups, enterprise, or workinglist variables outside of this function. I need to manipulate these results in other parts of my app. If I add print groups within the function, it acts correctly, but from outside the function I end up with a []. I've also tried to make the parse_csv a method of a class, adding a self and an init, with no success.

For reference I'm calling this from another file using parse_csv(csvfilename)It imports the file and reads it correctly.

I'm sure this is something very simple and very quick that I'm missing, but having stared at this so long, I just can't see it, any help would be GREATLY appreciated!

1
  • to ease the identification of your issue, please remove all unrelated code until you have a minimal example that reproduces the issue. (typically repl.it/UAZ with the output). As it is, your code may depend on external files such as csv content, which prevent anyone from reproducing your issue. Commented Jun 12, 2014 at 18:34

1 Answer 1

3

you should pass the lists as arguments to the function so that they can be modified. here's how it works:

foo = []

def f(arg):
  arg.append(1)
  arg.append(2)

f(foo)
print foo # output: [1, 2]

this works because the lists are mutable and you pass them by reference.

so you need to implement your function as follows:

def parse_csv(filename, gr, ent, work):
  ...
  gr.append(123)
  work.append(row)
  ...

then you can call it from every file like this:

groups      = []
enterprise  = []
workinglist = []

parse_csv(filename, groups, enterprise, workinglist)

print groups # should contain the data read from CSV
Sign up to request clarification or add additional context in comments.

1 Comment

that worked perfect!! Thank you very much. I can't upvote you until I get a few more points, but I definitely will once I have the opportunity!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.