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.
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']
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With