weird str error

Peter Otten __peter__ at web.de
Tue Sep 15 12:29:29 EDT 2009


daved170 wrote:

> Hi everybody,
> I'm using SPE 0.8.3.c as my python editor.
> I'm using the str() function and i got a very odd error.
> 
> I'm trying to do this: print str("HI")
> When i'm writing this line in the shell it prints: HI
> When it's in my code (it's the only line) i'm getting the following
> error:
> 
> file "c:\Python25\lib\local.py" line 242, in str
>    return format("%.12g",val)
> 
> file "c:\Python25\lib\local.py" line 145, in format
>    formatted = percent % value
>    TypeError, float argument required
> 
> any idea? It's worked for the entire day and unfortunately when i
> started my testing it raised this erroe/

local.py or locale.py? If the latter you are probably doing a star import:

from locale import *

This will replace the builtin str() with locale.str() which indeed requires 
a float:

>>> str("HI")
'HI'
>>> from locale import *
>>> str("HI")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/locale.py", line 244, in str
    return format("%.12g", val)
  File "/usr/lib/python2.5/locale.py", line 147, in format
    formatted = percent % value
TypeError: float argument required

As a general rule use the standard import

import locale

and then invoke the module's functions with an explicit prefix:

locale.setlocale(...)

It's a bit more to type, but it will save you a lot of trouble.

Peter

PS: When str() is the builtin str("HI") converts a string into a string 
which does not make much sense.




More information about the Python-list mailing list