How to fix this? ValueError: invalid literal for int() with base 10 error in Python

+1 vote

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?

Nov 16, 2018 in Python by Anirudh
• 2,080 points
77,237 views

3 answers to this question.

0 votes

The following are totally acceptable in python:

  • passing a string representation of an integer into int
  • passing a string representation of a float into float
  • passing a string representation of an integer into float
  • passing a float into int
  • passing an integer into float

But you get a ValueError if you pass a string representation of a float into int, or a string representation of anything but an integer (including empty string). If you do want to pass a string representation of a float to an int, as @katyhuff points out above, you can convert to a float first, then to an integer:

answered Nov 16, 2018 by Nymeria
• 3,540 points
0 votes

Python generates the error message you present in your question whenever you call the int() builtin function with a string argument that cannot be parsed as an integer; and, in fact, the error message shows you the precise string it was trying to parse as an integer: namely ‘0.25’.

How to fix the error? It depends on what you want to do.

If what you want is to parse and convert the string to a numeric value, this particular string clearly contains a numeric representation which is not an integer but a real. The way to “fix” the error in this case is to invoke the float() builtin function, which returns a floating point (real) value. If you really wanted an integer, despite having a real in the string, use int(float(your_value_here)). Note that this converts the string to a floating point value, which is then converted to an integer via truncation—that is, by discarding the fractional part. Applying these functions to ‘0.25’ will produce a result of 0. If, on the other hand, you wanted the floating point value, just use float().

Or, perhaps, you didn’t expect the ‘0.25’. In this case, find where that string comes from and fix the problem at the origin. Can’t help you there, though, as I don’t know your code and how that string got to the int() call.

answered Mar 14 by Facts
• 140 points
0 votes

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. 

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try...except block to handle this error.

Python2.x and Python3.x

Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .

With Python2.x , int(str(3/2)) gives you "1". With Python3.x , the same gives you ("1.5"): ValueError: invalid literal for int() with base 10: "1.5".

answered Apr 29 by warrenfelsh
• 140 points

Related Questions In Python

0 votes
1 answer
+1 vote
1 answer

How to print an error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 57,530 points
555 views
0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 57,530 points
5,073 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 957 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer

How should I write tests for Forms in Django with Python?

from django.tests import TestCase class MyTests(TestCase): ...READ MORE

answered Nov 20, 2018 in Python by Nymeria
• 3,540 points
101 views
0 votes
1 answer

How to parse date/time string with timezone abbreviated name in Python?

The parse() function in dateutil can't handle ...READ MORE

answered Nov 27, 2018 in Python by Nymeria
• 3,540 points
484 views