By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,368 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,368 IT Pros & Developers. It's quick & easy.

python 2.3, cvs module specific question

P: n/a
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.

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bernard Delmée wrote:
Hello, I can't seem to be able to specify the delimiter when building a
DictReader()

DictReader currently does not accept parameters for creating a dialect on
the fly. See Bug #792558.
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:
# ...


Another workaround might be (untested)

class MyDialect(csv.excel):
delimiter = ";"

inf = file('data.csv')
headers = inf.readline().split(';')
rd = csv.DictReader(inf, headers, dialect=MyDialect)

Of course your version should be more general, especially if you replace the
literal ';':

headers = inf.readline().split(d.delimiter)
Peter

Jul 18 '05 #2

P: n/a
Bernard> But this is rejected:

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

This is a known problem. For the time being, define a new dialect class and
pass it in. If you know what the delimiter is, it's easier to define a
class than call the sniffer.

class semi(csv.excel):
delimiter = ';'

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

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

No, it's just a bug in my implementation.

Skip

Jul 18 '05 #3

P: n/a
> class semi(csv.excel):
delimiter = ';'
Thanks, guys, I hadn't thought of this simple solution.
It solves my problem nicely.
No, it's just a bug in my implementation.


This module as it is, is very useful, Skip. Frankly with
the speed improvements, the csv, datetime and logging
modules, python 2.3 is a really satisfying release.
Rest assured that your effort does not go unnoticed.

Cheers,

Bernard.

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.