这是代码:
>>> z = u'\u2022'.decode('utf-8', 'ignore')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2022' in position 0: ordinal not in range(256)
为什么在使用 .decode 时会引发 UnicodeEncodeError?
为什么当我使用“忽略”时会出现错误?
当我第一次开始摆弄 python 字符串和 unicode 时,我花了一段时间才理解这些行话decode and encode也是,所以这是我的帖子here https://stackoverflow.com/questions/4474430/how-can-i-get-unicode-characters-from-a-url-parameter/4474614#4474614这可能有帮助:
考虑到decoding就像你从常规字节串中所做的那样to统一码和encoding就像你所做的那样from统一码。换句话说:
You de-code a str
生产一个unicode
字符串(在 Python 2 中)
and en-code a unicode
字符串来产生str
(在Python 2中)
So:
unicode_char = u'\xb0'
encodedchar = unicode_char.encode('utf-8')
encodedchar
将包含您的 unicode 字符,以所选编码显示(在本例中,utf-8
).
同样的原理也适用于 Python 3。de-code a bytes
对象产生一个str
目的。你呢en-code a str
对象产生一个bytes
object.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)