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(,).
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:
csv
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']
2.1m questions
2.1m answers
60 comments
57.0k users