Python将字符串转换为带有负数的浮点错误 [英] Python convert string to float error with negative numbers
本文介绍了Python将字符串转换为带有负数的浮点错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将以字符串形式存储的负数转换为浮点数?
How to convert a negative number stored as string to a float?
我在Python 3.6上遇到此错误,却不知道如何克服它.
Am getting this error on Python 3.6 and don't know how to get over it.
>>> s = '–1123.04'
>>> float(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '–1123.04'
推荐答案
您的字符串包含一个Unicode破折号,而不是ASCII连字符.您可以替换它:
Your string contains a unicode en-dash, not an ASCII hyphen. You could replace it:
>>> float('–1123.04'.replace('\U00002013', '-'))
-1123.04
这篇关于Python将字符串转换为带有负数的浮点错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文