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

Viewed 2884572

Why do I get this error?

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

21 Answers

I found a work around. Python will convert the number to a float. Simply calling float first then converting that to an int will work: output = int(float(input))

So if you have

floatInString = '5.0'

You can convert it to int with floatInInt = int(float(floatInString))

I recently came across a case where none of these answers worked. I encountered CSV data where there were null bytes mixed in with the data, and those null bytes did not get stripped. So, my numeric string, after stripping, consisted of bytes like this:

\x00\x31\x00\x0d\x00

To counter this, I did:

countStr = fields[3].replace('\x00', '').strip()
count = int(countStr)

...where fields is a list of csv values resulting from splitting the line.

My simple workaround to this problem was wrap my code in an if statement, taking advantage of the fact that an empty string is not "truthy":

Given either of these two inputs:

input_string = ""    # works with an empty string
input_string = "25"  # or a number inside a string

You can safely handle a blank string using this check:

if input_string:
   number = int(input_string)
else:
   number = None # (or number = 0 if you prefer)

print(number)

This could also happen when you have to map space separated integers to a list but you enter the integers line by line using the .input(). Like for example I was solving this problem on HackerRank Bon-Appetit, and the got the following error while compiling enter image description here

So instead of giving input to the program line by line try to map the space separated integers into a list using the map() method.

your answer is throwing errors because of this line

readings = int(readings)
  1. Here you are trying to convert a string into int type which is not base-10. you can convert a string into int only if it is base-10 otherwise it will throw ValueError, stating invalid literal for int() with base 10.

This seems like readings is sometimes an empty string and obviously an error crops up. You can add an extra check to your while loop before the int(readings) command like:

while readings != 0 or readings != '':
    readings = int(readings)

Another answer in case all of the above solutions are not working for you.

My original error was similar to OP: ValueError: invalid literal for int() with base 10: '52,002'

I then tried the accepted answer and got this error: ValueError: could not convert string to float: '52,002' --this was when I tried the int(float(variable_name))

My solution is to convert the string to a float and leave it there. I just needed to check to see if the string was a numeric value so I can handle it correctly.

try: 
   float(variable_name)
except ValueError:
   print("The value you entered was not a number, please enter a different number")

I faced this error in Django - Once in my models I had a DateTimeField() with some saved objects on it. then when I changed the field to DateField().

To solve this; I simply edited my Database(db.sqlite3) as I changed the date value from this format 2021-08-09 13:05:45 to this 2021-08-09, And that was all.

I got into the same issue when trying to use readlines() inside for loop for same file object... My suspicion is firing readling() inside readline() for same file object caused this error.

Best solution can be use seek(0) to reset file pointer or Handle condition with setting some flag then create new object for same file by checking set condition....

I had hard time figuring out the actual reason, it happens when we dont read properly from file. you need to open file and read with readlines() method as below:

with open('/content/drive/pre-processed-users1.1.tsv') as f:
    file=f.readlines()

It corrects the formatted output

Related