I have a CSV file that I generated through a csv.writer
object. Here is a simplified version of the code to generate the file:
output = open('out.txt', 'ab+')
writer = csv.writer(output, dialect='excel')
for item in dataset:
row = []
row.append(item['id'])
row.append(item['timestamp'])
values = []
for value in item['values']
values.append(value['name'])
row.append(values)
writer.writerow(row)
output.flush()
output.close()
A sample row in the CSV file looks like this:
457458856894140182,Mon Dec 02 11:36:48 +0000 2013,[u'Sugar', u'Rice', u'Bleach']
I've tried using a csv.reader
object to parse the data back into another script, but haven't been able to correctly parse the list in the third column. Python just sees the entire value [u'Sugar', u'Rice', u'Bleach']
as a string. For example, the code:
input = open('out.txt', 'rb')
reader = csv.reader(input, dialect='excel')
for row in reader:
print row[2][0]
input.close()
...just outputs a long list of [
on newlines.
How can I parse this CSV file correctly, to get assemble the structure in the third column back into a list in memory?
From the string
[u'Sugar', u'Rice', u'Bleach']
you can get the correspondinglist
object withast.literal_eval
: