[英]error encoding string as unicode in python 2.7?
我想在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
方法將不知道字符串的encode
方式,並假定它是ASCII。 然后,它嘗試將ASCII轉換為UTF-8,並在遇到ASCII字符集以外的字符時失敗。
另請注意,Python 3.3及更高版本支持此表示法。 在這種情況下,這基本上是一個禁忌,因為所有字符串都假定為unicode,但允許開發人員編寫與2.x和3.3+兼容的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.