How can I turn 000000000001 into 1?

I need to turn a formatted integer into a regular integer:

  • 000000000001 must be converted to 1
  • 000000000053 must be turned into 53
  • 000000965948 must be converted to 965948.

And so on.

It seems that a simple int(000000000015) leads to the number 13. I understand that there are strange things behind the scenes. What is the best way to do this every time?

+4
source share
4 answers

Leading zeros are octal. I assume that you are converting the string "000000013" and not the literal 000000013, so you should be able to convert them from base 10 integer to int("000000013",10)

If "harm has already been done", and they are literals that have already been converted to octal literals, you can use the following (beware, this is harmful and heresy :)

 int(("%o" % 00000013),10) 
+8
source

Numbers starting with 0 are considered octal .

 >>> 07 7 >>> 08 File "<stdin>", line 1 08 ^ SyntaxError: invalid token 

You can wrap a zero number in a string, then it should work.

 >>> int("08") 8 

int() takes an optional argument, which is the base, so the above would be equivalent:

 >>> int("08", 10) 8 
+11
source

Try the following:

 >>> int('00000000053', 10) 53 
+3
source
 if x = "0000000000000001": x = 1 
-eight
source

All Articles