Request for a change in the csv module.

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Mon Mar 12 20:04:08 EDT 2007


On Mar 12, 4:26 pm, Paulo da Silva <psdasil... at esotericaX.ptX> wrote:
> Hi.
>
> I have just seen that csv module, more exactly the Dialect class,
> does not have any variable to specify the "floating point" character!
> In portuguese this is ','. Not '.'. 3.1415 -> 3,1415.
> I think this is also the case of other languages/countries.
> If I am correct, i.e. if there is no such possibility, where can
> I file a request for a csv change? Excel, for example, automatically
> converts '.' to ',' and the separator from ',' to ';'.
>



Try using locale.format when writing:

import csv
import locale

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)


locale.atof and locale.atoi can convert a string back
to a number.

--
Hope this helps,
Steven





More information about the Python-list mailing list