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!