Jeff Blaine wrote: > It's been a year or so since I written Python code, so maybe > I am just doing something really dumb, but... > > Documentation > ============= > > class DictReader(csvfile[,fieldnames=None, > [,restkey=None[, restval=None[, dialect='excel' > [, *args, **kwds]]]]]) > > > Create an object which operates like a regular reader > but maps the information read into a dict whose keys > are given by the optional fieldnames parameter. If the > fieldnames parameter is omitted, the values in the > first row of the csvfile will be used as the fieldnames. > > Code > ==== > > import csv > > r = csv.DictReader('C:\Temp\Book1.csv')
Problem 1: """csvfile can be any object which supports the iterator protocol and returns a string each time its next method is called -- file objects and list objects are both suitable. If csvfile is a file object, it must be opened with the 'b' flag on platforms where that makes a difference.""" So, open the file, so that the next() method returns the next chunk of content, not the next byte in the name of the file. Note that the arg is called "csvfile", not "csvfilename". Problem 2: [OK in this instance, but that's like saying you have taken one step in a minefield and are still alive] backslashes and Windows file names: If you were to write 'c:\temp\book1.csv', it would blow up ... because \t -> tab and \b -> backspace. Get into the habit of *always* using raw strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns). You could use double backslashing 'C:\\Temp\\Book1.csv' but it's uglier. > print r.next() > # EOF > > Output > ====== > > {'C': ':'} HTH, John -- http://mail.python.org/mailman/listinfo/python-list