strings

Discussion in 'Python' started by Scribe, Sep 4, 2003.

  1. Scribe

    Scribe Guest

    Hi
    Somewhat new to Python and would appreciated some help in selecting the
    best builtins to achieve what I want.
    I pass a string to a function, it is either
    empty
    spaces
    alpha and digits
    or
    a decimal ie. 5,789.88
    if it is a decimal I want to return the number without the commer any
    others return 0.
    Should be simple or maybe I am.
    Any help apreciated.
     
    Scribe, Sep 4, 2003
    #1
    1. Advertising

  2. Scribe

    Nick Welch Guest

    On Thu, Sep 04, 2003 at 04:18:38AM +0000, Scribe wrote:
    > if it is a decimal I want to return the number without the commer any
    > others return 0.


    "commer"?

    str.isdigit() should do most of it for you.

    def foo(mystr):
    if mystr.isdigit():
    return "the number without the commer (??) :)"
    else:
    return 0

    --
    Nick Welch aka mackstann | mack @ incise.org | http://incise.org
    Penguin Trivia #46:
    Animals who are not penguins can only wish they were.
    -- Chicago Reader 10/15/82
     
    Nick Welch, Sep 4, 2003
    #2
    1. Advertising

  3. Scribe

    Peter Otten Guest

    Scribe wrote:

    > Hi
    > Somewhat new to Python and would appreciated some help in selecting the
    > best builtins to achieve what I want.
    > I pass a string to a function, it is either
    > empty
    > spaces
    > alpha and digits
    > or
    > a decimal ie. 5,789.88
    > if it is a decimal I want to return the number without the commer any
    > others return 0.
    > Should be simple or maybe I am.
    > Any help apreciated.


    import locale

    locale.setlocale(locale.LC_ALL, ("en", None))

    def strtofloat(s):
    try:
    return locale.atof(s)
    #return float(s.replace(",", ""))
    except ValueError:
    return 0.0


    for s in "a 0 12 123.4 12,345.67 12,24a".split():
    print s, "->", strtofloat(s)

    Output:
    a -> 0.0
    0 -> 0.0
    12 -> 12.0
    123.4 -> 123.4
    12,345.67 -> 12345.67
    12,24a -> 0.0

    Peter
     
    Peter Otten, Sep 4, 2003
    #3
  4. Scribe wrote:

    > Hi
    > Somewhat new to Python and would appreciated some help in selecting the
    > best builtins to achieve what I want.
    > I pass a string to a function, it is either
    > empty
    > spaces
    > alpha and digits
    > or
    > a decimal ie. 5,789.88
    > if it is a decimal I want to return the number without the commer any
    > others return 0.
    > Should be simple or maybe I am.
    > Any help apreciated.


    def scribefun(astring):
    try: return int(float(astring.replace(',','')))
    except ValueError: return 0

    This assumes you want to return e.g. 5789 for '5,789.88', i.e.,
    an integer truncating the "decimal" (otherwise omit the int()).

    Only possible issue is if the "alpha and digits" specifically
    include an 'e' as the only "alpha", which builtin float() would
    take as an indication of exponential notation; so for example
    this would return 100, not 0, for '1e2'. If this is a problem
    (unclear from your specs) it ain't all that hard to fix of course.


    Alex
     
    Alex Martelli, Sep 4, 2003
    #4
    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. Kurt Krueckeberg
    Replies:
    2
    Views:
    732
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    395
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    529
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    587
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
Loading...

Share This Page