Octal number problem

sik0fewl xxdigitalhellxx at hotmail.com
Thu Feb 20 16:07:04 CET 2003


Gary Duncan wrote:
> 
> Python has the quaint behaviour of interpreting
> a numeric string with a leading zero as octal, a bit like C.
> 
> How does one disable this behaviour, ie get it to
> treat such strings as a "normal" decimal number ?
> 
> I've had a quick search via Google for an answer and nothing
> lept out at me. Lutz and Aschers "Learning Python" book had the
> 'wink-wink/nudge-nudge' comment "Python interprets them  [numbers with
> leading 0s]  as octal constants, which usually don't work as you'd expect!'
> 
> (Was that from "Life of Brian" ? the exclamation mark was theirs, btw ;)
> 
> (In an item a few days ago, I complained of the unhelpful
>   diagnostic one gets from Python when one inputs say "0999";
>   ie "Syntax Error: invalid token". But thats a side issue)
> 
> 
> A bit of background:-
> 
>     I have a list of 5-digit decimal numbers, which was constructed
> with leading zeroes where necessary so that the list is sorted by magnitude
> using Unix 'sort' (otherwise '00200' would appear before '1').

Well, if you have it as a string in the form "00200" then int("00200") 
would work fine.. if not then I'm not sure. I'm sure there's a way you 
could convert it back to decimal, but as you said "0999" == bad.

--
Ryan





More information about the Python-list mailing list