On 5/22/2008 12:54:55 PM, "Ned Deily" <[EMAIL PROTECTED]> wrote:

> In article <[EMAIL PROTECTED]>,
>  Jeremy Reichman <[EMAIL PROTECTED]> wrote:
> 
>> I came across the locale module, which looks as if it will do what I want
>> and perform number formatting (with groupings separated by commas) for a
>> report I'm trying to generate. It always seems that there's a module for
>> everything I want to do.
>> 
>> I'm seeking output like:
>> 
>> '1,234.56'
>> '12,345,678,910,111,213,141,516'
>> 
>> However, the default Python 2.5.1 in Leopard is not formatting numbers for
>> me in a script. I've also seen the same results from the shell. For example:
>> 
>> $ python -c 'import locale; print locale.getdefaultlocale(); print
>> locale.format("%8.2f", 1234.56, 3)'
>> ('en_US', 'UTF8')
>>  1234.56
>> $ python -c 'import locale; print locale.getdefaultlocale(); print
>> locale.format("%d", 12345678910111213141516, 3)'
>> ('en_US', 'UTF8')
>> 12345678910111213141516
>> 
>> I've tried this with and without a call to 'locale.setlocale(locale.LC_ALL,
>> "")' as I have seen in some examples.
> 
> It looks like you just need to set LC_NUMERIC, as in:
> 
> Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
>>>> import locale
>>>> print locale.localeconv()
> {'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127,
> 'frac_digits': 127, 'thousands_sep': '', 'n_sign_posn': 127,
> 'decimal_point': '.', 'int_curr_symbol': '', 'n_cs_precedes': 127,
> 'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '',
> 'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [],
> 'p_cs_precedes': 127, 'positive_sign': '', 'grouping': []}
>>>> print locale.format("%8.2f", 1234.56, True)
>  1234.56
>>>> locale.setlocale(locale.LC_NUMERIC, 'en_US')
> 'en_US'
>>>> print locale.localeconv()
> {'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127,
> 'frac_digits': 127, 'thousands_sep': ',', 'n_sign_posn': 127,
> 'decimal_point': '.', 'int_curr_symbol': '', 'n_cs_precedes': 127,
> 'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '',
> 'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [],
> 'p_cs_precedes': 127, 'positive_sign': '', 'grouping': [3, 3, 0]}
>>>> print locale.format("%8.2f", 1234.56, True)
> 1,234.56
>>>> print locale.format("%8.2f", 1234.56, False)
>  1234.56

I later found that if I set LC_ALL (which I had tried) but specify "en_US"
as my locale (which I hadn't), it worked, too. At some point I got an error
about the C locale, and that's when I picked "en_US."

>>> import locale
>>> locale.getlocale()
(None, None)
>>> locale.format("%d", 12345678910111213141516, True)
'12345678910111213141516'
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.UTF-8'
>>> locale.getlocale()
('en_US', 'UTF8')
>>> locale.format("%d", 12345678910111213141516, True)
'12,345,678,910,111,213,141,516'
>>> locale.setlocale(locale.LC_ALL, "en_US")
'en_US'
>>> locale.getlocale()
('en_US', 'ISO8859-1')
>>> locale.format("%d", 12345678910111213141516, True)
'12,345,678,910,111,213,141,516'
>>> locale.format("%8.2f", 1234.56, 3)
'1,234.56'

So, trying everything again, even the locale.setlocale(locale.LC_ALL, "")
call helped, and locale.format() worked as expected afterwards -- but not
before.

I'm continually astounded by how much is in the standard library.


-- 
Jeremy


_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to