Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (19.9k points)

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?

1 Answer

0 votes
by (25.1k points)

The int() method is used to convert a numeric representation int an integer. It can take a number in any data type such as float, string etc and convert it into an integer number.

You are getting a ValueError as Exception, which means you passed a value that cannot be parsed into an integer value. In your case after looking at the error message it seems that it is because an empty string is being passed as parameter in. To overcome this issue you can wrap your code inside a try except block and skip over invalid entries that might throw an exception. E.G.:

for data in ['', '11', 11.15]:





This code will loop over a list of data and convert it to int if possible and if not then it will catch the exception and won't do anything with it.

Browse Categories