正如标题所说,我希望它读取“?”从 Excel 工作表中读取,当超过它时将其计为 0。
for eachLine in train:
tempList=eachLine.split(',')
avg0=float(tempList[0])
ageL.append(avg0)
avg1=float(tempList[1])
sL.append(avg1)
avg2=float(tempList[2])
rbsL.append(avg2)
avg3=float(tempList[3])
fbsL.append(avg3)
avg4=float(tempList[4])
ogtL.append(avg4)
avg5=float(tempList[5])
hemo.append(avg5)
我知道它很丑,但它对我有用。任何帮助将不胜感激,谢谢。
最佳答案
如果我明白你想要做什么,你应该创建一个函数来解析每个单元格并返回浮点值或检查问号。
def parse_cell(cell):
try:
return float(cell)
except ValueError:
if cell == "?":
return 0.0
raise
然后用这个函数替换每个 float 调用,即
avg0 = parse_cell(tempList[0])
关于python - 如何使 '?' 等于一个数字,所以当我浏览列表时。它将把它算作零。 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37230805/