[Csv] How to use a non-default delimiter with DictReader?

Bernard Delmée bdelmee at advalvas.be
Thu Aug 21 20:35:17 CEST 2003


Hello,

I am not sure this is the right place to post, else let me know.
I mean, is this address dedicated to the development of the CSV  
module, or to its mere usage as well?

Anyway, 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 Csv mailing list