Most pythonic way to format a number with commas?

Emile van Sebille emile at fenx.com
Fri Jul 12 07:54:30 EDT 2002


Alex Martelli
> Emile van Sebille wrote:
> >
> > It's buried in locale...
>
> "buried" is a loaded word...:-)

Yes... sorry ;-)  That's just what it felt like the first time I needed
it.  And reflects in part a more recent run-in with locale.

[clarifying and expounding remarks snipped]

The other reason for my use of "buried" is my recent discovery of the
source of a problem I experienced after writing a simple repeatable
obfustication function to scramble text like customer names and
addresses, and that initially depended on string.letters.

ActivePython 2.1, build 210 ActiveState)
based on Python 2.1 (#15, Apr 19 2001, 10:28:27) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'English_United States.1252'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
\x83\x8a\x8c\x8e\x9a\x9c\x9e\x9f\xc0\xc1\xc2\xc3\xc4
\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1
\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf
\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec
\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb
\xfc\xfd\xfe\xff'
>>>

As this ran within zope, it was first imported as part of a product
before setlocale happened elsewhere within zope.  Only once zope was up
and running the problem surfaced.  The cvs version of zope shows that
the errant module has been reworked and no longer uses locale.

Then again, this may be common knowlege I'm only now finding out.  But
at least I'm forewarned for the next time I need to know my abc's _and_
put comma's in numbers.  ;-)

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list