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
161,812 views

8 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,520 points
if you give code example, it will be easier to us.

Hello Maiul Islam Faruqi,

Here is the code as explained by @ Nymeria:

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

Hope it helps!!

Thank You!!

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

The top paragraph how do i fix my coding

Hi, @Sophie,

Could you please post your code snippet so that I can verify what wrong is happening! And what exact error you are facing.

The above-mentioned explanation has been given with different possible solutions to come out of the error.

what do you use for non integers then?

Hi, @K,

Could you please elaborate your query, what exactly you want to ask regarding?

0 votes

Hi,

Even I was going through the same problem and what I found is understanding the basics is one of the immediate hacks to come out of these kinds of errors. Here we need to understand "What does int () do in Python?". 

What the function does is, the int() function converts the specified value into an integer number. The int() function returns an integer object constructed from a number or string x, or return 0 if no arguments are given. A number or string to be converted to integer object.

answered Jul 1 by Gitika
• 41,360 points

edited Jul 2 by Gitika
0 votes

Hello @Everyone,

I want to bring one alternate perspective to focus on why does this error happening? What does invalid literal for int with base 10 mean in Python? 

Let's understand this first, 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. But you get a ValueError: invalid literal for int() with base 10, if you pass a string representation of a float into an int , or a string representation of anything but an integer (including the empty string).

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

answered Jul 1 by Roshni
• 4,630 points
+1 vote

Hey,

I tried to look for a few other hacks from where I found my concept regarding the issue wasn't clear. I kept on trying "How do I fix ValueError invalid literal for int <UNK> with base 10?" 

My issue got resolve when I understood the applied concept where "If you still need an int and can't change the literal, try using float() to parse the string, then use int() on the result to convert it from a float to an int. This will truncate the value parsed from the string to an integer, and will avoid the ValueError as long as the string contains some real numeric value." 

answered Jul 1 by keshav
+1 vote

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

Has to be used!

answered Aug 17 by pakainfo
• 200 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.

answered Aug 28 by Pistle
• 1,000 points

Related Questions In Python

0 votes
1 answer

Error is "invalid literal for int() with base 10: ' ' "

This error is caused because we try ...READ MORE

answered 5 days ago in Python by Gitika
• 41,360 points
33 views
0 votes
1 answer
0 votes
1 answer

How to create a unicode string in python with the string eg: This is a string?

Hey, @Roshni, It is very simple to execute, ...READ MORE

answered Jun 23 in Python by Gitika
• 41,360 points
76 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 1,453 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,085 views
+1 vote
1 answer

How to get the home directory using ‘~’ in Python?

Hello, @Roshni, You need to import the os module, ...READ MORE

answered Jun 26 in Python by Gitika
• 41,360 points
94 views
0 votes
3 answers

How can I use grep in Python?

Hello,  Here goes a short demonstration on How ...READ MORE

answered Jul 1 in Python by Nagaraj

edited Jul 2 28,234 views