weird str error

Discussion in 'Python' started by daved170, Sep 15, 2009.

  1. daved170

    daved170 Guest

    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/
    daved170, Sep 15, 2009
    #1
    1. Advertising

  2. daved170

    Peter Otten Guest

    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.
    Peter Otten, Sep 15, 2009
    #2
    1. Advertising

  3. daved170

    Dave Angel Guest

    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/
    >
    >

    I should start by asking why you're using using str() on a string
    literal? It would seem to just be a waste of time to convert a string
    to a string.

    Next, I'll have to insist that you copy/paste your error messages to the
    email; by retyping it you got the wrong file name. The file with that
    line in it is locale.py

    So now, you have a *locale* problem. Somebody other than I will have to
    help. And all the effort I put in before noticing your typo is wasted.

    You'd better describe your environment more thoroughly. Python version,
    OS, locale, and just how you're running this script. You said it's "the
    only line" so are you omitting the shebang line? Perhaps you're running
    on Windows? If so, you can copy from a cmd window using
    right-click-drag, at least if Quick-Edit is on.


    DaveA
    Dave Angel, Sep 15, 2009
    #3
  4. daved170

    daved170 Guest

    On Sep 15, 6:29 pm, Peter Otten <> wrote:
    > daved170 wrote:
    > > Hi everybody,
    > > I'm using SPE 0.8.3.c as my python editor.
    > > I'm using thestr() function and i got a very odd error.

    >
    > > I'm trying to do this: printstr("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, instr
    > >    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 builtinstr() 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, instr
    >     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: Whenstr() is the builtinstr("HI") converts a string into a string
    > which does not make much sense.- Hide quoted text -
    >
    > - Show quoted text -


    Hi Peter,
    Thanks for your answer.
    I'll clearify myself. I'm using QString cause I have a GUI app. I want
    to convert it to python string. It works well for several month and
    suddenly two days ago it didn't. I'm not using localE at all (i'm not
    importing it).
    After your answer I tried it but it still didn't work.
    Any other ideas?
    Thanks
    DaveD
    daved170, Sep 17, 2009
    #4
  5. daved170

    Peter Otten Guest

    daved170 wrote:

    > On Sep 15, 6:29 pm, Peter Otten <> wrote:
    >> daved170 wrote:
    >> > Hi everybody,
    >> > I'm using SPE 0.8.3.c as my python editor.
    >> > I'm using thestr() function and i got a very odd error.

    >>
    >> > I'm trying to do this: printstr("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, instr
    >> > 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 builtinstr() 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, instr
    >> 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: Whenstr() is the builtinstr("HI") converts a string into a string
    >> which does not make much sense.- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Hi Peter,
    > Thanks for your answer.
    > I'll clearify myself. I'm using QString cause I have a GUI app. I want
    > to convert it to python string. It works well for several month and
    > suddenly two days ago it didn't. I'm not using localE at all (i'm not
    > importing it).
    > After your answer I tried it but it still didn't work.


    You mean you don't have any star imports in the module causing error?

    > Any other ideas?


    Run your script from the command line (not your editor). Then give the
    complete traceback.

    Grep through the libraries you use for 'from locale' and 'import locale' to
    identify the one that uses the locale module. You can identify modules used
    by your script by running

    python -v script.py

    Aside: if you use a version control system and check in frequently it gets
    easier to identify a modification that introduced an error. Highly
    recommended.

    Peter
    Peter Otten, Sep 17, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    470
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    328
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,659
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    296
    Chris
    Aug 11, 2008
Loading...

Share This Page