csv.py sucks for Decimal

MRAB python at mrabarnett.plus.com
Thu Apr 22 20:03:17 EDT 2010


Phlip wrote:
> Pythonistas:
> 
> This is not a question so much as registering a complaint.
> 
> When I use the CSV library, with QUOTE_NONNUMERIC, and when I pass in
> a Decimal() object, I must convert it to a string. _Not_ a float,
> because that might cause the rounding errors that Decimal() seeks to
> avoid. (We use Decimal for Currency, naturally.)
> 
> Then product feed readers, who were written to ass-ume Excel output,
> kack when they see quotes around a price field, such as "19.95".
> 
> I have fixed this by searching-and-replacing the quotes out of the
> intermediate file. Please understand I am not asking after any such
> low-level fix - and the search for an alternate CSV module, without
> this bug, will indeed begin very soon! And, of course, we will switch
> to XML wherever possible.
> 
> I'm pointing out that QUOTE_NONNUMERIC would work better with an
> option to detect numeric-as-string, and absolve it. That would allow
> Decimal() to do its job, unimpeded.
> 
> Also, the CSV format should set its configurations per-column, not
> just per-file.
> 
It might be a stupid question, but have you tried passing in the
Decimal() object itself?



More information about the Python-list mailing list