我有一个Unicode减号的Python字符串u'\u221220'
aka“ -20” 。
当尝试转换为浮点数时,
>>> a = u'\u221220'
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\u2212' in position 0: invalid decimal Unicode string
使用Python 2和
>>> a = u'\u221220'
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '−20'
使用Python 3。
如何在Python 2和Python 3中正确转换u'\u221220'
为float -20.0
?便携式解决方案将是很棒的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句