Tuesday, July 10, 2012

csv.DictReader in python

I am playing around getting python to parse simple CSV files. In the past, it would get quite difficult no matter which language I choose to do it. PERL does some useful parsing of it, but python is dead simple. It becomes even simpler when usign the csv.DictReader. The following snippet of code illustrates the point.

from optparse import OptionParser
import csv

parser = OptionParser()
parser.add_option("-f", "--filename", dest="filename", help="the filename of the csv file")

(options, args)=parser.parse_args()

if options.filename != None:
        csv_filename = str(options.filename)
        csv_filename = "foobar.csv"
print "csv file is " + csv_filename

inputfile = open(csv_filename, "rb")
csvReader = csv.DictReader(inputfile, fieldnames=['create_ts','title','url','id','hn_discussion'], delimiter=',', quotechar='"')

for row in csvReader:
        print row['id'] + " " + row['url']


The first part just parses for an input from the console for a filename of the csv file.

The second part is the interesting part where it parses the file as per the given fieldnames. The fieldnames were given during the call of the csv.DictReader.

By the way, this snippet of code is more prototype rather than production. Much error checking is missing and exception handling is missing, so beware.