Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error:

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

It is reading the first line but can't convert it to an integer.

What can I do to fix this problem?

The code:

file_to_read = raw_input("Enter file name of tests (empty string to end program):") try:     infile = open(file_to_read, 'r')     while file_to_read != " ":         file_to_write = raw_input("Enter output file name (.csv will be appended to it):")         file_to_write = file_to_write + ".csv"         outfile = open(file_to_write, "w")         readings = (infile.readline())         print readings         while readings != 0:             global count             readings = int(readings)             minimum = (infile.readline())             maximum = (infile.readline()) 
like image 425
Sarah Cox Avatar asked Dec 03 '09 17:12

Sarah Cox


People also ask

How do I fix this ValueError invalid literal for int with base 10 error in Python?

ValueError: invalid literal for int() with base 10 occurs when you convert the string or decimal or characters values not formatted as an integer. To solve the error, you can use the float() method to convert entered decimal input and then use the int() method to convert your number to an integer.

What means invalid literal for int () with base 10?

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 literal error in Python?

Conclusion. 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

Just for the record:

>>> int('55063.000000') Traceback (most recent call last):   File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '55063.000000' 

Got me here...

>>> int(float('55063.000000')) 55063 

Has to be used!

like image 92
FdoBad Avatar answered Oct 12 '22 20:10

FdoBad