Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > int() and leading zeros in Python 2.6

Reply
Thread Tools

int() and leading zeros in Python 2.6

 
 
Pete Forman
Guest
Posts: n/a
 
      11-12-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed) -./\.- the opinion of Schlumberger or
http://petef.22web.net -./\.- WesternGeco.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-12-2008
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

 
Reply With Quote
 
 
 
 
Pete Forman
Guest
Posts: n/a
 
      11-12-2008
Peter Otten <(E-Mail Removed)> 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
(E-Mail Removed) -./\.- the opinion of Schlumberger or
http://petef.22web.net -./\.- WesternGeco.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
format a number with leading and trailing zeros absmienk@hotmail.com Perl Misc 4 08-15-2007 01:18 PM
iostreams - removing leading zeros and controlling exponent field width Anjo Gasa C++ 0 01-30-2007 09:05 PM
Hexadecimal formatting with leading zeros?! Dmitry Bond. ASP .Net 1 10-13-2005 08:55 AM
Formatting an integer with leading zeros GIMME Java 5 02-13-2004 05:01 PM
Re: CSV for Excel - Problem with Leading Zeros Luke Zhang [MSFT] ASP .Net 0 06-26-2003 01:34 PM



Advertisments