ValueError: could not convert string to float: '[108.62257164103005]'

ValueError: could not convert string to float: '[108.62257164103005]'

我正在尝试将列表转换为浮点数,因此首先将其转换为字符串,但是当我尝试将字符串转换为浮点数时,它不起作用并出现错误: ValueError:无法将字符串转换为浮点数:'[108.62257164103005]' 对不起,如果我是菜鸟,请提前致谢。

我不确定括号是干什么用的,但你可以像这样将 string 转换为 float

str1 = '108.62257164103005'
str2 = float(str1)

print(type(str2))
print(str2)

给你:

<class 'float'>
108.62257164103005

或者,如果您想从 list 转到 float,只需对其进行迭代

str0 = ['108.62257164103005']
for i in str0:
    print(float(i))

您的字符串中似乎有方括号。

您可以先 运行 像这样清理您的字符串: string.replace(']','').replace('[','') 要么 string[1:-1] 这将删除第一个和最后一个字符