Problems with conversion of values in strings to integers

Erik Max Francis max at alcyone.com
Mon Oct 6 15:19:18 EDT 2003


Jørgen Cederberg wrote:

>  >>> int('10.1')
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> ValueError: invalid literal for int(): 10.1
> 
> This is quite frustating. The only solution I can see is to do:
>  >>> int(float('10.1'))
> 10
> 
> Is this the only solution or are there any other way to do this?

That's probably the best way.  What Python is doing is right, in my
opinion; "10.1" is not an integer, so when trying to convert it to one,
you're getting an error.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Tell me the truth / I'll take it like a man
\__/  Chante Moore




More information about the Python-list mailing list