Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Parse CSV Correctly

I am very new to Python. I want to parse a csv file such that it will recognize quoted values - for example

1997,Ford,E350,"Super, luxurious truck"

should be split as

('1997', 'Ford', 'E350', 'Super, luxurious truck')

and NOT

('1997', 'Ford', 'E350', '"Super', ' luxurious truck"')

the above is what I get if I use something like str.split(,).

How do I do this? Also would it be best to store these values in an array or some other data structure? because after I get these values from the csv I want to be able to easily choose, lets say any two of the columns and store it as another array or some other data structure.

like image 441
cornerstone Avatar asked Sep 06 '12 09:09

cornerstone


2 Answers

You should use the csv module:

import csv reader = csv.reader(['1997,Ford,E350,"Super, luxurious truck"'], skipinitialspace=True) for r in reader:     print r 

output:

['1997', 'Ford', 'E350', 'Super, luxurious truck'] 
like image 62
akhter wahab Avatar answered Sep 17 '22 13:09

akhter wahab


The following method worked perfectly

d = {} d['column1name'] = [] d['column2name'] = [] d['column3name'] = []  dictReader = csv.DictReader(open('filename.csv', 'rb'), fieldnames = ['column1name', 'column2name', 'column3name'], delimiter = ',', quotechar = '"')  for row in dictReader:     for key in row:         d[key].append(row[key]) 

The columns are stored in dictionary with the column names as the key.

like image 36
cornerstone Avatar answered Sep 17 '22 13:09

cornerstone