int() and leading zeros in Python 2.6

Discussion in 'Python' started by Pete Forman, Nov 12, 2008.

  1. Pete Forman

    Pete Forman Guest

    I'm holding off installing Python 2.6, waiting for some packages to
    become available for it. I wonder if someone could tell me the best
    way to avoid future problems parsing decimal integers with leading
    zeros.

    >>> int('09')

    9

    That works in 2.5 but will break in 2.6 AFAIK as int() is being
    changed to use Numeric Literal syntax. It will give a syntax error as
    the leading 0 will force an octal radix and the 9 will be out of
    range. Will this avoid the breakage?

    >>> int('09', 10)

    9

    Or should I use this uglier variation that needs 2.2.2 or later?

    >>> int('09'.lstrip('0'))

    9

    Is the documentation for int([x[, radix]]) correct? I'd say that the
    default for radix has become 0.

    http://docs.python.org/library/functions.html#int

    --
    Pete Forman -./\.- Disclaimer: This post is originated
    WesternGeco -./\.- by myself and does not represent
    -./\.- the opinion of Schlumberger or
    http://petef.22web.net -./\.- WesternGeco.
     
    Pete Forman, Nov 12, 2008
    #1
    1. Advertising

  2. Pete Forman

    Peter Otten Guest

    Pete Forman wrote:

    > I'm holding off installing Python 2.6, waiting for some packages to
    > become available for it. I wonder if someone could tell me the best
    > way to avoid future problems parsing decimal integers with leading
    > zeros.


    You can have multiple versions of python simultaneously.

    >>>> int('09')

    > 9


    This works for 2.x and 3.0.

    2.6 will accept two prefixes "0o" and "0" when you give 0 as the radix
    argument. 3.0 will only accept "0o" and raise a ValueError for "0". None of
    this affects you.

    > That works in 2.5 but will break in 2.6 AFAIK as int() is being
    > changed to use Numeric Literal syntax. It will give a syntax error as
    > the leading 0 will force an octal radix and the 9 will be out of
    > range. Will this avoid the breakage?
    >
    >>>> int('09', 10)

    > 9


    That's unnecessary.

    > Or should I use this uglier variation that needs 2.2.2 or later?
    >
    >>>> int('09'.lstrip('0'))

    > 9


    And that's cargo cult code.

    > Is the documentation for int([x[, radix]]) correct?


    Yes.

    > I'd say that the default for radix has become 0.


    You can say that, but you're wrong.

    Peter
     
    Peter Otten, Nov 12, 2008
    #2
    1. Advertising

  3. Pete Forman

    Pete Forman Guest

    Peter Otten <> writes:
    > you're wrong.


    Indeed I am, sorry for the waste of time.
    --
    Pete Forman -./\.- Disclaimer: This post is originated
    WesternGeco -./\.- by myself and does not represent
    -./\.- the opinion of Schlumberger or
    http://petef.22web.net -./\.- WesternGeco.
     
    Pete Forman, Nov 12, 2008
    #3
    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. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,053
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,249
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. r.magdeburg

    leading zeros - wrong int

    r.magdeburg, Dec 1, 2003, in forum: C++
    Replies:
    2
    Views:
    1,739
    red floyd
    Dec 1, 2003
  4. Anjo Gasa
    Replies:
    0
    Views:
    359
    Anjo Gasa
    Jan 30, 2007
  5. Replies:
    4
    Views:
    124
Loading...

Share This Page