[Csv] This surprised me
Skip Montanaro
skip at pobox.com
Sat Feb 8 06:35:56 CET 2003
This code surprised me:
>>> class foo: pass
...
>>> csv.register_dialect("excel", foo)
>>> csv.get_dialect("excel")
<__main__.foo instance at 0x5309f8>
>>> import StringIO
>>> rdr = csv.reader(StringIO.StringIO("1,2,3\r\n"))
>>> list(rdr)
[['1', '2', '3']]
>>> rdr = csv.reader(StringIO.StringIO("1,2,3\r\n"), dialect="excel")
>>> list(rdr)
[['1', '2', '3']]
>>> rdr = csv.reader(StringIO.StringIO("1,2,3\r\n"), dialect=foo)
>>> list(rdr)
[['1', '2', '3']]
>>> rdr = csv.reader(StringIO.StringIO("1,2,3\r\n"), dialect=foo)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.3/site-packages/csv.py", line 27, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
_csv.Error: Dialect did not validate: delimiter not set, quotechar not set, lineterminator not set, doublequote setting must be True or False, skipinitialspace setting must be True or False
Why didn't it complain anywhere that 'foo' was worthless as a dialect until
the last statement?
Skip
More information about the Csv
mailing list