Valueerror: Invalid Literal For Int() With Base 10: '' In Python Code
I have the following line of code that opens a document called document.txt that looks somewhat like this. 3 4 5 6 6 3 2 8 9 4 6 with open('document.txt','r') as f: for li
Solution 1:
I'm guessing that there's a blank line at the end of the file. This, when stripped, becomes an empty string, which cannot be parsed as an int.
Deleting the blank line will make this work, but you can think of this as an exercise in defensive programming. As one of my favorite professors once told me, "a good programmer is someone who looks both ways before crossing a one-way street".
Solution 2:
You might have to replace the line skip which is something like this in python
\n
If I were you my code would be:
f = open('document.txt','r')
temp = f.readlines()
array = []
for i in temp:
i = i.replace("\n","")
i = i.split(" ")
for x in range(len(i)):
i[x] = int(i)
array.append(i)
Solution 3:
I have also gone through this problem. The simple thing you can do is either use
strip()
method or replace()
method.
Hope this will help you to find the answer.
Post a Comment for "Valueerror: Invalid Literal For Int() With Base 10: '' In Python Code"