Read and Write CSV files with Python

Since CSV (comma-separated-values) files are supported by almost every statistics and/or math tool, I sticked to use the format for all my data collections.

If you need further evidence, check out the (incomplete) list of natively CSV supporting applications:

Apart from those, a major benefit of CSV files is that you can edit them with any editor, as they are plain text files. Unfortunately, some applications have poor support for such files, e.g. Matlab and Python. Fortunately, as the file structure is so easy, you can add CSV support pretty easily. In the following I will show you my solution for Python.

Reading CSV Files with Python

The following snippet imports the CSV module and reads a CSV file including its header and data.

import csv

# open csv file
csvfile = open( "inputfile.csv", "rb" )

# sniff into 10KB of the file to check its dialect
dialect = csv.Sniffer().sniff( csvfile.read( 10*1024 ) )
csvfile.seek(0)

# read csv file according to dialect
reader = csv.reader( csvfile, dialect )

# read header
header = reader.next()

# read data into memory
data = [row for row in reader]

# close input file
csvfile.close()

Writing CSV Files with Python

The following snippet writes a CSV file including its header and data.

# open output file
outfile = open( options.outfile, "wb" )

# get a csv writer
writer = csv.writer( outfile )

# write header
writer.writerow(header)

# write data
[ writer.writerow(x) for x in data ]

# close file
outfile.close()

That’s it for now :)
iss