A=[None, '0.50', '2', '4', '6', '0', '0', '0', '0.00', '0', '0', '0', '5', '1', '5', '5', '1', '1', '0', '1', '2', '2', '2', '0 (1:1)', '0', '0', '2', '2', '0', '0', '1', '2', '0']
B=[]
for elem in A:
if elem is not None:
B.append(float(elem.strip("''")))
else:
B.append(elem)
print B
我有一个列表,其中包含上面显示的值。当我尝试使用熊猫添加到Excel表格时,它将被添加为文本而不是float或int。我想删除引号,以便它不会被视为excel表格中的文本,我知道它可以使用split来完成,如果我尝试将所有这些转换为float,它将引发此'0( 1:1)'元素说无效文字为float。我该如何处理?如何在将列表中的文字转换为浮点时处理异常?
你关心物品'0(1:1)'吗? – ILostMySpoon
那么你想用'0(1:1)'来做什么? – khelwood
你提到熊猫 - 你看过[pandas.to_numeric](https://pandas-docs.github.io/pandas-docs-travis/generated/pandas.to_numeric.html)并指定错误='胁迫'吗? –