473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

int() and leading zeros in Python 2.6

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
pe*********@westerngeco.com -./\.- the opinion of Schlumberger or
http://petef.22web.net -./\.- WesternGeco.
Nov 12 '08 #1
2 7056
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

Nov 12 '08 #2
Peter Otten <__*******@web.dewrites:
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
pe*********@westerngeco.com -./\.- the opinion of Schlumberger or
http://petef.22web.net -./\.- WesternGeco.
Nov 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4695
by: Stian | last post by:
Hi, I'm sort of new to Python and I've just discovered the amazing formatting using the % operator. One thing I can't figure out is how to format numbers so that they get leading zeros, for example...
6
13749
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
2
2676
by: r.magdeburg | last post by:
//please tell me why... //and give me a hint to solve the problem with leading zeros. //snippet #include <iostream.h> #include <conio.h> int main() { int zahl = 0; cout << "Give me an int...
5
11477
by: samik_tanik | last post by:
I need to export a datagrid to Excel. I could did this. But, also need to keep the leading zeros in the data. How can I acheive this? Any help would be appreciated. -- Thanking you in...
1
4536
by: mmmgood1 | last post by:
Help, I'm linking an excel spreadsheet in access and I have datafields with leading zeros (01021). When the file is linked in access, I get a #num in the field with the leading zeros. The zeros...
6
5277
by: Clint Stowers | last post by:
Using A2k Exporting a query to a CSV file. The problem is any text fields (i.e. 000345) lose any leading zeros. Exporting to an excel file this problem does not exist. Tried to create a...
5
3461
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
6
7730
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for...
0
4062
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL =...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.