Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: invalid literal for int() with base 10: '808.666666666667'

Tags:

python

I have a script that parses data from a csv file. The following line is giving me problems:

countData.append([timeStamp,int(my_csv[row][5])])

It gives me the following error:

ValueError: invalid literal for int() with base 10: '808.666666666667' 

The line of the csv file is as follows:

2013-06-12 15:09:00,svcName,0,0,10,808.666666666667

I have tried running int(808.666666666667) in a python prompt and it works fine.

like image 491
imagineerThat Avatar asked Jul 09 '13 20:07

imagineerThat


People also ask

What is invalid literal for int () with base 10 in python?

invalid literal for int() with base 10. The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.

What is an invalid literal in Python?

The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. To solve this problem, you can use the float() method to convert a floating-point number in a string to an integer.


1 Answers

From help on int:

int(x, base=10) -> int or long

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base.

So, '808.666666666667' is an invalid literal for int for any base, use:

>>> int(float('808.666666666667' ))
808

int(808.666666666667) runs fine because you're passing a float to it, not a string literal.

like image 143
Ashwini Chaudhary Avatar answered Sep 29 '22 10:09

Ashwini Chaudhary