CSV module, DictReader problem (bug?)

Fredrik Lundh fredrik at pythonware.com
Wed Nov 1 21:47:18 CET 2006


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')
> 	print r.next()
> 	# EOF

here's the documentation for the regular reader, from Python 2.5:

     reader(...)
         csv_reader = reader(iterable [, dialect='excel']
                                 [optional keyword args])
             for row in csv_reader:
                 process(row)

         The "iterable" argument can be any object that returns a line
         of input for each iteration, such as a file object or a list.
         ...

so the reader is simply looping over the characters in your filename.  try

     r = csv.DictReader(open('C:\Temp\Book1.csv'))

instead.

</F>




More information about the Python-list mailing list