Request for a change in the csv module.

Diez B. Roggisch deets at nospam.web.de
Tue Mar 13 04:18:58 EDT 2007


Paulo da Silva schrieb:
> attn.steven.kuo at gmail.com escreveu:
>> On Mar 12, 4:26 pm, Paulo da Silva <psdasil... at esotericaX.ptX> wrote:
> ...
> 
>> locale.setlocale(locale.LC_NUMERIC, 'pt_BR.ISO8859-1')
>> csv_writer = csv.writer(open("foo.csv","w"), dialect='excel')
>> rows = (('testing', 1.23), ('testing', 2.34))
>> formatted_rows = ((string, locale.format('%g', number)) for
>> (string,number) in rows)
>> csv_writer.writerows(formatted_rows)
> 
> 
> That works but it is a pain to use.

Why? I think it's straightforward.

> May be I'll sublass csv or even I'll write one myself.
> It would be much better to be able to specify an additional
> variabel to the Dialect class and change csv.

I don't think so. The csv module is about the subtleties that can occur 
when parsing textual data with possible escape chars and the like, and 
creating that data.

But IHMO its up to the user to feed it with just textual data. Because 
automatically converting numbers falls flat on it's face in case of 
special requirements like a limit on the number of digits to render and 
the like. Better do that in a simple and straightforward preprocessing 
state, as shown above.

The same is essentially true for dates as well, btw. How do you want to 
deal with them?

Diez



More information about the Python-list mailing list