[Tutor] Need to convert 1,987,087,234,456 to an int

Dick Moores rdm at rcblue.com
Thu Aug 16 00:43:02 CEST 2007


If a line in a script has
n = "1,987,087,234,456"
It's simple to convert the string, "1,987,087,234,456" to an int, but 
I can't figure out how to do it for
n = 1,987,087,234,456  # no quotes
(I want to be able to type in big ints using commas--much easier to 
see that I've typed correctly)

Python sees 1,987,077,234,456 as a tuple:
 >>>type(1,987,077,234,456)
<type 'tuple'>

But:
 >>>type(1,987,087,234,456)
      File "<input>", line 1
        type(1,987,087,234,456)
                            ^
SyntaxError: invalid token

(My wild guess is that 087 is octal.)

Anyway,

I need to convert things such as 1,987,077,234,456 to ints. This will do it:
(m can also be an int such as 1987077234456)
if type(m) == tuple:
         mAsStr = ""
         for part in m:
             part = str(part)
             mAsStr += part
         m = int(mAsStr)

So this is fine as long as none of the parts of the tuple is of the 
form 08X (where X is from 0-9). What to do?

Thanks,

Dick Moores





More information about the Tutor mailing list