t = ["3.4","7","","-1.3","-5",""]
如何将此列表转换为整数并浮动以进行进一步的分析?
到目前为止我已经完成了这项工作,但我收到了错误。
t = ["3.4","7","","-1.3","-5",""]
def integer_float(string):
try:
return int(string)
except ValueError:
return float(string)
for i in t:
integer_float(i)
我收到了错误
ValueError: could not convert string to float:
答案 0 :(得分:1)
因为你不能将空字符串解析为int或float。尝试:
int(string or "0")