csv module strangeness.
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Wed Aug 30 16:49:51 EDT 2006
In <44f5e870$0$8814$88260bb3 at free.teranews.com>, tobiah wrote:
> I'm trying to create a cvs.reader object using a custom dialect.
>
> The docs are a little terse, but I gather that I am supposed
> to subclass cvs.Dialect:
>
> class dialect(csv.Dialect):
> pass
>
> Now, the docs say that all of the attributes have reasonable
> defaults, but instantiating the above gives:
>
> Traceback (most recent call last):
> File "<stdin>", line 15, in ?
> File "/usr/local/lib/python2.4/csv.py", line 39, in __init__
> raise Error, "Dialect did not validate: %s" % ", ".join(errors)
> _csv.Error: Dialect did not validate: delimiter character not set, quotechar not set, lineterminator not set, doublequote parameter must be True or False, skipinitialspace parameter must be True or False, quoting parameter not set
>
> So I look at the source. The Dialect class is very simple,
> and starts with:
>
> class Dialect:
> _name = ""
> _valid = False
> # placeholders
> delimiter = None
> quotechar = None
> escapechar = None
> doublequote = None
> skipinitialspace = None
> lineterminator = None
> quoting = None
>
> So, it's no wonder that it fails its validate() call.
> The only thing that I can think of to do is to set
> these on the class itself before instantiation:
>
> ###############################################
> import csv
>
> class dialect(csv.Dialect):
> pass
>
> dialect.delimiter = "\t"
> dialect.quotechar = '"'
> dialect.lineterminator = "\n"
> dialect.doublequote = True
> dialect.skipinitialspace = True
> dialect.quoting = csv.QUOTE_MINIMAL
That's possible but why didn't you follow the way `csv.Dialect` set the
class attributes?
class MyDialect(csv.Dialect):
delimiter = '\t'
lineterminator = '\n'
# and so on…
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list