Parsing strings -> numbers

Tuang tuanglen at hotmail.com
Tue Nov 25 04:27:05 EST 2003


Skip Montanaro <skip at pobox.com> wrote in message news:<mailman.1040.1069710514.702.python-list at python.org>...
> 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/locale.py
> 
>     MODULE DOCS
>         http://www.python.org/doc/current/lib/module-locale.html
> 
>     DESCRIPTION
>         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.
> 
>     ...
> 
>     FUNCTIONS
>         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\locale.py", line 179, in atoi
    return atof(str, int)
  File "C:\Python2321\lib\locale.py", line 175, in atof
    return func(str)
ValueError: invalid literal for int(): -12,345
>>> locale.getdefaultlocale()
('en_US', 'cp1252')
>>> locale.atoi("-12345")
-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?

Thanks.




More information about the Python-list mailing list