atof problem.

Rod.Haper at sage.nelesautomation.com.bbs Rod.Haper at sage.nelesautomation.com.bbs
Fri Jul 14 19:00:01 EDT 2000


Also make sure that your locale is not set to something useful - the
default is usually 'C' which leave most of the locale parameters unset.
Here is an example of setting the locale to en_US showing how it sets
the various locale parameters:

>>>
>>> import locale
>>> locale.setlocale(locale.LC_ALL)
'C'
>>>
>>> locale.localeconv()
{'grouping': [127], 'currency_symbol': '', 'n_sign_posn': 127,
'p_cs_precedes': 127, 'n_cs_precedes': 127, 'mon_grouping': [],
'n_sep_by_space': 127, 'decimal_point': '.', 'negative_sign': '',
'positive_sign': '', 'p_sep_by_space': 127, 'int_curr_symbol': '',
'p_sign_posn': 127, 'thousands_sep': '', 'mon_thousands_sep': '',
'frac_digits': 127, 'mon_decimal_point': '', 'int_frac_digits': 127}
>>>
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>>
>>> locale.localeconv()
{'grouping': [3, 3, 0], 'currency_symbol': '$', 'n_sign_posn': 1,
'p_cs_precedes': 1, 'n_cs_precedes': 1, 'mon_grouping': [3, 3, 0],
'n_sep_by_space': 0, 'decimal_point': '.', 'negative_sign': '-',
'positive_sign': '', 'p_sep_by_space': 0, 'int_curr_symbol': 'USD ',
'p_sign_posn': 1, 'thousands_sep': ',', 'mon_thousands_sep': ',',
'frac_digits': 2, 'mon_decimal_point': '.', 'int_frac_digits': 2}
>>>
>>> s = '123,456,789.2'
>>> s
'123,456,789.2'
>>> locale.atof(s)
123456789.2
>>>





richard_chamberlain wrote:
>
> Hi William,
>
> Yes you could use the locale module to do what you want:
>
> import locale
> s="123,456,789.2"
> print locale.atof(s)
>
> As Bjorn said you'll have to get the locale module from somewhere.
>
> Otherwise do something like below:
>
> import string
> def rcatof(num):
>     try:
>         flt=float(string.replace(num,',',''))
>         return flt
>     except:
>         return None
>
> >>> import myScript
> >>> myScript.rcatof('123,456.2')
> 123456.2
>
> Richard
>
> William Dandreta <wjdandreta at worldnet.att.net> wrote in message
> news:yUqb5.977$tI4.52638 at bgtnsc05-news.ops.worldnet.att.net...
> > I am having a problem with the atof function. I tried to convert 1,142.00
> to
> > a float but the comma causes a problem. I resolved the problem by removing
> > the comma but there must be a better way.
> >
> > I looked in the documentation and there was mention of the $LANG
> > environmental variable but it did not tell haw to use it. Also, it
> mentioned
> > settings could be made using the functions in locale module. I tried to
> > import the locale module but I get an error message that the _locale
> module
> > doesn't exist.
> >
> > Anyone have a clue about this?
> >
> > Bill
> >
> >

--
+----------------------------------------------------------------------+
| Rod Haper, Senior Systems Analyst      |  Neles Automation           |
| Phone: 713-346-0652 - direct           |  SCADA Solutions, Inc.      |
|        713-939-0393 - fax              |  7000 Hollister             |
| Rod.Haper at sage.nelesautomation.com     |  Houston, Texas 77040, USA  |
+----------------------------------------------------------------------+



More information about the Python-list mailing list