Help, I keep getting ValueError: invalid literal for int() with base 10: '' when I try to extract an integer from a string!
帮助,我一直得到ValueError:对于带有基数10的int()的无效文字:''当我尝试从字符串中提取整数时!
from string import capwords
import sys,os
import re
def parseint(List):
newlist = [i for i in List if isinstance(i, int)]
return newlist
def getint(string):
number = [int(x) for x in string.split("-")]
return number
file=open('./Class 1/usr_score.data','r')
text=file.read()
def get_num(x):
return int(''.join(ele for ele in x if ele.isdigit()))
split = text.split(",")
split.sort(key = lambda k : k.lower())
for i in split:
print(i)
print ('---------------------------------------')
list1=[]
for i in split:
list1.append(str(i))
num_list1=[]
for i in list1:
ints = re.findall(r'\b\d+\b', i)
#ints = getint(i)[0]
for i in ints:
int1=i
num_list1.append(i)
#num_list1 = parseint(list1)
num_list=num_list1
for i in num_list:
print(i)
The file usr_score.data contains:
文件usr_score.data包含:
user-1,aaa-1,usr-3,aaa-4,
What my code is is that it contains scores for a game and I want my program to sort them into alphabetical order. Can someone please fix my problem?
我的代码是它包含游戏的分数,我希望我的程序按字母顺序排序。有人可以解决我的问题吗?
Note: Some of the code in the program has not been used.
注意:程序中的某些代码尚未使用。
2 个解决方案
#1
Your input data has a ',' at the end, which causes split() to generate an empty string in addition to the scores:
您的输入数据最后有一个',',这会导致split()除了得分之外还生成一个空字符串:
['user-1', 'aaa-1', 'usr-3', 'aaa-4', '']
int('')
doesn't work; you should either remove that empty string, or deal with it.
int('')不起作用;你应该删除那个空字符串,或处理它。
#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()不能接受空字符串,这是一个无效的参数。当将字符串作为int时,您需要测试字符串是否为空。你可以在列表理解中这样做:
[int(x) if not (x.isspace() or x == '') else 0 for 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.
如果您愿意,可以将0替换为None或其他结果,但这基本上总是使用string.isspace()函数检查字符串不仅仅是空白字符,还要确保x不是空字符串。
#1
Your input data has a ',' at the end, which causes split() to generate an empty string in addition to the scores:
您的输入数据最后有一个',',这会导致split()除了得分之外还生成一个空字符串:
['user-1', 'aaa-1', 'usr-3', 'aaa-4', '']
int('')
doesn't work; you should either remove that empty string, or deal with it.
int('')不起作用;你应该删除那个空字符串,或处理它。
#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()不能接受空字符串,这是一个无效的参数。当将字符串作为int时,您需要测试字符串是否为空。你可以在列表理解中这样做:
[int(x) if not (x.isspace() or x == '') else 0 for 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.
如果您愿意,可以将0替换为None或其他结果,但这基本上总是使用string.isspace()函数检查字符串不仅仅是空白字符,还要确保x不是空字符串。