Problems with conversion of values in strings to integers

  • Thread starter =?ISO-8859-1?Q?J=F8rgen_Cederberg?=
  • Start date
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Hi,

using Python 2.2.1 on Windows and QNX I'm having trouble understandig
why int() raises a ValueError exception upon converting strings.
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:10

Is this the only solution or are there any other way to do this?

Regards
Jorgen Cederberg
 
P

Peter Otten

Jørgen Cederberg said:
Hi,

using Python 2.2.1 on Windows and QNX I'm having trouble understandig
why int() raises a ValueError exception upon converting strings.

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:
10

Is this the only solution or are there any other way to do this?


There is more than one way to convert a string representing a fp number to
an integer:
2

I think it's good that Python does not make the decision for you. Once you
have decided which route to go you can easily wrap it into a helper
function, e. g.

def toInt(s):
try:
return int(s)
except ValueError:
return int(round(float(s)))

(When dealing with user input, things are a little more complicated, as the
decimal separator changes between locales)

Peter
 
K

Karl Scalet

Jørgen Cederberg said:
Hi,

using Python 2.2.1 on Windows and QNX I'm having trouble understandig
why int() raises a ValueError exception upon converting strings.

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:
10

depending on what you actually want,might be appropriate.
But if you really want the "floor"-value,
nothing is wrong with your approach.

Karl
 
?

=?ISO-8859-15?Q?J=F8rgen_Cederberg?=

Peter said:
There is more than one way to convert a string representing a fp number to
an integer:

2

I think it's good that Python does not make the decision for you. Once you
have decided which route to go you can easily wrap it into a helper
function, e. g.

def toInt(s):
try:
return int(s)
except ValueError:
return int(round(float(s)))

(When dealing with user input, things are a little more complicated, as the
decimal separator changes between locales)

Thanks a lot for the function. It solves the problem elegantly.

/Jorgen
 
E

Erik Max Francis

Jørgen Cederberg said:
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:
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.
 
E

Erik Max Francis

Karl said:
depending on what you actually want,
might be appropriate.
But if you really want the "floor"-value,
nothing is wrong with your approach.

round actually takes a float as an argument, not a string, so you
probably meant

int(round(float('10.1')))
 
K

Karl Scalet

Erik said:
Karl Scalet wrote:




round actually takes a float as an argument, not a string, so you
probably meant

int(round(float('10.1')))

yes, of course, sorry

Karl
 
B

Bengt Richter

There is more than one way to convert a string representing a fp number to
an integer:
Indeed, but what if the OP wants something like C's atoi? E.g.,
(playing with MinGW and MSYS -- pretty neat, BTW ;-)

[14:35] /c/pywk/clp/atoi>cat pratoi.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if(argc<2){
printf("Usage: pratoi strings ...\n");
} else {
int i;
char sbuf[32];
for(i=1;i<argc;++i){
snprintf(sbuf,32,"'%s'",argv);
printf("%8s => %d\n", sbuf, atoi(argv));
}
}
return 0;
}
[14:35] /c/pywk/clp/atoi>gcc pratoi.c -o pratoi
[14:35] /c/pywk/clp/atoi>pratoi 0 1 -1 +1 123 4.5 -5.6 -1.2e3 0x11 011 " -3xx" " -xx"
'0' => 0
'1' => 1
'-1' => -1
'+1' => 1
'123' => 123
'4.5' => 4
'-5.6' => -5
'-1.2e3' => -1
'0x11' => 0
'011' => 11
' -3xx' => -3
' -xx' => 0

Note the 'way different result on -1.2e3 in particular. Also does the OP want to ignor
octal and hex or reject them?

atoi looks like it just strips leading spaces, takes and optional sign, and computes
a decimal from as many decimal digits as it can then find (including none as ok).

I'll leave that as an exercise for python ;-)
2

I think it's good that Python does not make the decision for you. Once you
have decided which route to go you can easily wrap it into a helper
function, e. g.

def toInt(s):
try:
return int(s)
except ValueError:
return int(round(float(s)))

(When dealing with user input, things are a little more complicated, as the
decimal separator changes between locales)
There's that too ;-)

Regards,
Bengt Richter
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Bengt said:
There's that too ;-)

Actually my problem arose using the Scale widget in Tkinter. The small
program below demonstrates the problem.

The first line with Scale doesn't work, as the IntVar(!), tries to
convert a string from Tkinter, that is in fact a float. This results
from the Tkinter function getint, which is the same as int.

--------------------------
from Tkinter import *

def onScale(value):
print "The value is", value,"and its type is", type(value)
print "The variable is", var.get(),"and its type is", type(var.get())

root = Tk()
var = IntVar()

# This does'nt work. The value inside the Scale is now a float
represented in
# string, which int() can't handle.
#s = Scale(root, length=100, bd=1, digit=3, from_ = -10,
#to=10,variable=var, command=onScale)

# This works.
s = Scale(root, length=100, bd=1, from_ = -10, to=10,variable=var,
command=onScale)
s.pack()

root.mainloop()
--------------------------

One might argue that it doesn't make sense to use the Scale with digits
together with an IntVar, but in my case to from_ and to values can both
be integers and floats.

Regards
Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top