我有这个代码:
"'{}'".format(u'ES SIOUF_1')
在 Python 2 中运行时,我收到以下错误:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 2: ordinal not in range(128)
从 Python 3 运行的相同代码给出:
>>> "'ES\xa0SIOUF_1'"
我也不需要。我需要的是:
>>> "'ES SIOUF_1'"
我阅读了许多关于 Python 中“编码”和“解码”字符的问题,以及 Python 2 和 3 之间在这方面的一些差异。
但是,我真的不理解它们,我想可能为两个版本的 Python 解决这个问题。
我注意到的事情是:
type(u'ES SIOUF_1')
给出:
>>> <type 'unicode'> # PYTHON 2
>>> <class 'str'> # PYTHON 3