python 2.3, cvs module specific question

Bernard Delmée bdelmee at advalvas.REMOVEME.be
Wed Sep 3 16:52:22 EDT 2003


Hello, I can't seem to be able to specify the delimiter when building a 
DictReader()

I can do:

     inf = file('data.csv')
     rd = csv.reader( inf, delimiter=';' )
     for row in rd:
         # ...

But this is rejected:

     inf = file('data.csv')
     headers = inf.readline().split(';')
     rd = csv.DictReader( inf, headers, delimiter=';' )
     for row in rd:
         # ...

The DictReader constructor fails with a TypeError:
init() got an unexpected keyword argument 'delimiter'
Maybe I am missing something here?

One rather convoluted workaround is the following:

     inf = file('data.csv')
     d = csv.Sniffer().sniff(s)
     inf.seek(0)
     headers = inf.readline().split(';')
     rd = csv.DictReader( inf, headers, dialect=d )
     for row in rd:
         # ...

If DialectReader does indeed not accept the optional "fmtparam"
then at least the documentation needs fixing. But then again
I may just be misreading it....

TIA,

Bernard.





More information about the Python-list mailing list