List to CSV in 10 Seconds – Python CSV Module Quick Reference

Here’s the syntax to write to a CSV file:

import csv

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:
And this takes you straight to the examples in the documentation.


Another useful writeup of the CSV module.

[tags]Python, csv, csv file, comma delimited, comma seperated values, comma seperated, Python documentation[/tags]

Comments are closed.