Python - Valueerror: Invalid Literal For Int() With Base 10: ''
Help, I keep getting ValueError: invalid literal for int() with base 10: '' when I try to extract an integer from a string! from string import capwords import sys,os import re def
Solution 1:
Your input data has a ',' at the end, which causes split() to generate an empty string in addition to the scores:
['user-1', 'aaa-1', 'usr-3', 'aaa-4', '']
int('')
doesn't work; you should either remove that empty string, or deal with it.
Solution 2:
int() can't take an empty string, that's an invalid parameter for it. You'll need to test for if a string is empty when getting it as an int. You can do that in a list comprehension like this:
[int(x) ifnot(x.isspace()orx== '') else0for x in string.split("-")]
You can replace 0 with None or some other result if you'd prefer, but this basically always checks that a string isn't just whitespace characters using the string.isspace() function and also makes sure x isn't an empty string.
Post a Comment for "Python - Valueerror: Invalid Literal For Int() With Base 10: ''"