List to CSV in 10 Seconds – Python CSV Module Quick Reference
Here’s the syntax to write to a CSV file:
import csv w=csv.writer(file(r'C:\greg.csv','wb'),dialect='excel') some_values=[[1,2,3],['A','B','C'],[4,'"5"','ab,c']] w.writerows(some_values)
Make sure you use ‘wb’ when you open the file. Otherwise I’ve found that it will put a blank line between each row at least when Excel opens it.
Here’s the syntax for reading a csv file (from the documentation):
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
print row[0], row[-1]
And here’s the link to the full documentation:
http://docs.python.org/lib/module-csv.html
And this takes you straight to the examples in the documentation.
Update:
Another useful writeup of the CSV module.
[tags]Python, csv, csv file, comma delimited, comma seperated values, comma seperated, Python documentation[/tags]