Parse CSV containing list in a column

2.4k views Asked by At

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?

1

There are 1 answers

0
damienfrancois On BEST ANSWER

From the string [u'Sugar', u'Rice', u'Bleach'] you can get the corresponding list object with ast.literal_eval:

>>> a="[u'Sugar', u'Rice', u'Bleach']"
>>> import ast
>>> ast.literal_eval(a)
[u'Sugar', u'Rice', u'Bleach']
>>> ast.literal_eval(a)[1]
u'Rice'
>>>