Pythonic use of CSV module to skip headers?

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Sat Dec 4 10:02:45 EST 2004


Skip Montanaro wrote:
> I'm not sure what the use of TABs as delimiters has to do with the OP's
> problem.

Not much. :) I just happen to use tabs more often than commas, so my 
subclass defaults to

> You can create a subclass of DictReader that plucks the first line out as a
> set of titles:
> 
>     class SmartDictReader(csv.DictReader):
>         def __init__(self, f, *args, **kwds):
>             rdr = csv.reader(*args, **kwds)
>             titles = rdr.next()
>             csv.DictReader.__init__(self, f, titles, *args, **kwds)
> 
> Is that what you were suggesting?

Exactly.

> I don't find the couple extra lines of
> code in my original example all that cumbersome to type though.

If you started about half of the programs you write with those extra 
lines, you might <wink>. I'm a strong believer in OnceAndOnlyOnce.

Thanks to Nick Coghlan for pointing out that I no longer need do this in 
Python 2.4.
-- 
Michael Hoffman



More information about the Python-list mailing list