Parsing strings -> numbers
tuanglen at hotmail.com
Tue Nov 25 10:27:05 CET 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:
> locale - Locale support.
> MODULE DOCS
> 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.
> 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
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
ValueError: invalid literal for int(): -12,345
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