Pythonic use of CSV module to skip headers?

Nick Coghlan ncoghlan at email.com
Sat Dec 4 13:39:27 CET 2004


Michael Hoffman wrote:
> I deal so much with tab-delimited CSV files that I found it useful to 
> create a subclass of csv.DictReader to deal with this, so I can just write:
> 
> for row in tabdelim.DictReader(file(filename)):
>     ...
> 
> I think this is a lot easier than trying to remember this cumbersome 
> idiom every single time.

Python 2.4 makes the fieldnames paramter optional:
   "If the fieldnames parameter is omitted, the values in the first row of the 
csvfile will be used as the fieldnames."

i.e. the following should work fine in 2.4:

for row in csv.DictReader(file(filename)):
   print sorted(row.items())

Cheers,
Nick.



More information about the Python-list mailing list