Python-ValueError:以10为底的int()的无效文字:'' [英] Python - ValueError: invalid literal for int() with base 10: ''
问题描述
帮助,当我尝试从字符串中提取整数时,我会不断收到ValueError:int()的无效文字,基数为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 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)
文件usr_score.data包含:
The file usr_score.data contains:
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.
推荐答案
您的输入数据的末尾有一个',',这会导致split()除了得分以外还生成一个空字符串:
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('')
不起作用;您应该删除该空字符串,或者对其进行处理.
int('')
doesn't work; you should either remove that empty string, or deal with it.
这篇关于Python-ValueError:以10为底的int()的无效文字:''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!