Re: csv.py sucks for Decimal

2010-04-25 Thread John Machin
On Apr 23, 9:23 am, Phlip phlip2...@gmail.com wrote:

 When I use the CSV library, with QUOTE_NONNUMERIC, and when I pass in
 a Decimal() object, I must convert it to a string.

Why must you? What unwanted effect do you observe when you don't
convert it?

 the search for an alternate CSV module, without
 this bug, will indeed begin very soon!

What bug?

 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.

Decimal()'s job is to create an instance of the decimal.Decimal class;
how is that being impeded by anything in the csv module?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: csv.py sucks for Decimal

2010-04-23 Thread Phlip
On Apr 22, 5:03 pm, MRAB pyt...@mrabarnett.plus.com wrote:

 It might be a stupid question, but have you tried passing in the
 Decimal() object itself?

Yep. Nope. Might as well (we ain't working today).

But sorry, as usual, for my tone, and thanks all for playing!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: csv.py sucks for Decimal

2010-04-23 Thread Phlip
On Apr 22, 6:15 pm, Jerry Hill malaclyp...@gmail.com wrote:

 10,10.0,10.00,10

 That's an int, a float, a Decimal and a string, all of which appear to
 be formatted as I would expect.

When you point your finger 'cause your plan fell thru
you got three more fingers, pointing back at you! --Dire Straights
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: csv.py sucks for Decimal

2010-04-22 Thread Robert Kern

On 4/22/10 6:23 PM, 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.


Patches may be submitted here:

  http://bugs.python.org

--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


Re: csv.py sucks for Decimal

2010-04-22 Thread MRAB

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?
--
http://mail.python.org/mailman/listinfo/python-list


Re: csv.py sucks for Decimal

2010-04-22 Thread Jerry Hill
On Thu, Apr 22, 2010 at 8:03 PM, MRAB pyt...@mrabarnett.plus.com wrote:
 It might be a stupid question, but have you tried passing in the
 Decimal() object itself?

MRAB's suggestion works for me in python 3.1.2:

import csv, io
from decimal import Decimal

d = Decimal(10.00)
o = io.StringIO()
w = csv.writer(o, quoting=csv.QUOTE_NONNUMERIC)

w.writerow([10, 10.00, d, 10])

print(o.getvalue())


10,10.0,10.00,10

That's an int, a float, a Decimal and a string, all of which appear to
be formatted as I would expect.

-- 
Jerry
-- 
http://mail.python.org/mailman/listinfo/python-list