Below i have a string which represents a single row pulled from a csv file. Each column is separated by a comma and the value is wrapped in "". What is the simplest way to parse out the value from each column in python?
"Mr","Bob","","Boberton","","President","","","","Blah, Inc. of Iowa","blah blah blah","","Grand Island","Hall County","NE","68801","7228","United States","308-111-1111","","","P.O. BOX 1111","","Grand Island","Hall County","NE","11111","1111","United States","","40.00000","-98.0000","Gasoline service stations","11111","1010101010","","","false","SINGLE_LOCATION","","","","","","","","","No","No","No","Owns","No","No","","","55.125905","","0.052369","","100","","100","0","","","1971","Low Risk","Convenience Stores & Truck Stops","1111111","1111111","111111","1111111"
The above data is all one big string that needs to be split into columns so i can get the values. I'm not asking how to load a csv file. Already got that part figured out.
Python has a module for that:
http://docs.python.org/library/csv.html
import csv, sys
filename = 'some.csv'
with open(filename, 'rb') as f:
reader = csv.reader(f)
try:
for row in reader:
print row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
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