我想在Python 2.7中打印字符串的unicode版本。它在Python 3中工作正常。但是在python 2.7中,出现以下错误:
x="strings are now utf-8 \u03BCnico\u0394é!"
Python 3:
print('Python', python_version())
print(x)
Python 3.4.1
strings are now utf-8 μnicoΔé!
Python 2.7
>>> x='strings are now utf-8 \u03BCnico\u0394é!'
>>> x.encode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 38: ordinal not in range(128)
编辑:我尝试了followimg:
>>> x = u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'
>>> x
u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'
>>> x.encode("utf-8")
'strings are now utf-8 \\u03BCnico\\u0394\xc3\x83\xc2\xa9!'
>>> x
u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'
我看不到编码发生
编辑2:
>>> x=u'strings are now utf-8 \u03BCnico\u0394é!'
>>> x.encode("utf-8")
'strings are now utf-8 \xce\xbcnico\xce\x94\xc3\xa9!'
>>> b=x.encode("utf-8")
>>> b
'strings are now utf-8 \xce\xbcnico\xce\x94\xc3\xa9!'
>>>
在Python 2.x中,您需要使用unicode文字:
x=u"strings are now utf-8 \u03BCnico\u0394é!"
encode
否则,该方法将不知道字符串的编码方式,并假定它是ASCII。然后,它尝试将ASCII转换为UTF-8,并在遇到ASCII字符集以外的字符时失败。
还请注意,Python 3.3及更高版本支持此表示法。在这种情况下,这基本上是一个禁忌,因为所有字符串都假定为unicode,但允许开发人员编写与2.x和3.3+兼容的代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句