#!/usr/bin/python
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)
else:
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']
inputfile.close()
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.