Choffee/ thoughtsplurge/ posts/ 2008/ 04/ 14/ CSV files in python

CSV files in python

I spent some time today working out how to deal with commas in a quoted csv files ( ie. 123,"sd,sd","sdggg" and that comma in the middle) when it suddenly dawned on me that I would not be the first to have tried this. Low and behold the python csv module:

import csv
csvdata = csv.reader(open("yourfile.csv", "rb"))
for line in csvdata:
    print line[2]

As per usual python comes with batteries included so it's always worth checking the library reference first before wasting some time thinking.

Tags: ?programming ?python
blog comments powered by Disqus Valid XHTML 1.0 Strict Valid CSS! Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.