Parsing strings -> numbers

Tuang tuanglen at
Tue Nov 25 10:27:05 CET 2003

Skip Montanaro <skip at> wrote in message news:<mailman.1040.1069710514.702.python-list at>...
> tuanglen> I've been looking all over in the docs, but I can't figure out
>     tuanglen> how you're *supposed* to parse formatted strings into numbers
>     tuanglen> (and other data types, for that matter) in Python.
> Check out the locale module.  From "pydoc locale":
>     Help on module locale:
>     NAME
>         locale - Locale support.
>     FILE
>         /Users/skip/local/lib/python2.4/
>         The module provides low-level access to the C lib's locale APIs
>         and adds high level number formatting APIs as well as a locale
>         aliasing engine to complement these.
>     ...
>         atof(str, func=<type 'float'>)
>             Parses a string as a float according to the locale settings.
>         atoi(str)
>             Converts a string to an integer according to the locale settings.
>     ...

Thanks for taking a shot at it, but it doesn't appear to work:

>>> import locale
>>> locale.atoi("-12,345")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python2321\lib\", line 179, in atoi
    return atof(str, int)
  File "C:\Python2321\lib\", line 175, in atof
    return func(str)
ValueError: invalid literal for int(): -12,345
>>> locale.getdefaultlocale()
('en_US', 'cp1252')
>>> locale.atoi("-12345")

Given the locale it thinks I have, it should be able to parse
"-12,345" if it can handle formats containing thousands separators,
but apparently it can't.

If Python doesn't actually have its own parsing of formatted numbers,
what's the preferred Python approach for taking taking data, perhaps
formatted currencies such as "-$12,345.00" scraped off a Web page, and
turning it into numerical data?


More information about the Python-list mailing list