Python CSV DictWriter Problems

Gabriel Cooper gabriel.cooper at mediapulse.com
Mon May 3 11:22:29 EDT 2004


Is there a known-bug in csv.DictWriter? It is ignoring my attempts to 
specify a Dialect. It doesn't even error if you give it anything 
invalid. I can send it the registered string-name of the dialect, a 
string of gibberish, a dialect object instance, a dialect class 
object... it accepts all and ignores all.

Take for example a default-created Dialect, excel-tab:

Python 2.3.3 (#1, Apr 14 2004, 01:02:50)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import csv
 >>> csv.list_dialects()
['excel-tab', 'excel']
 >>> csv.excel_tab.delimiter
'\t'
 >>> a = 
csv.DictWriter(open('/root/quickbooks/testout.iif','w'),['a','b'],"excel-tab") 

 >>> a.writer.dialect.delimiter
','
 >>> a = 
csv.DictWriter(open('/root/quickbooks/testout.iif','w'),['a','b'],csv.excel_tab()) 

 >>> a.writer.dialect.delimiter
','
 >>> a = 
csv.DictWriter(open('/root/quickbooks/testout.iif','w'),['a','b'],"no 
one is listening")
 >>> a.writer.dialect.delimiter
','
 >>> a = 
csv.DictWriter(open('/root/quickbooks/testout.iif','w'),['a','b'],csv.excel_tab) 

 >>> a.writer.dialect.delimiter
','
 >>> a.writer.dialect = csv.excel_tab()
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
TypeError: readonly attribute
 >>>

Currently I have to go through and manually set each of the dialect's 
attributes individually, like so:
 >>> a.writer.dialect.delimiter = '\t'
(etc.)




More information about the Python-list mailing list