这不是解码失败.这是因为您正在尝试将结果显示到控制台.使用print时,它使用默认编码ASCII编码字符串.不要使用打印,它应该工作.
>>> a=u'really long string containing \\u20ac and some other text'
>>> type(a)
>>> a.decode('unicode-escape')
u'really long string containing \u20ac and some other text'
>>> print a.decode('unicode-escape')
Traceback (most recent call last):
File "", line 1, in
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 30: ordinal not in range(128)
我建议使用IDLE或其他可以输出unicode的解释器,那么你就不会遇到这个问题.
更新:请注意,这与少一个反斜杠的情况不同,它在解码期间失败,但具有相同的错误消息:
>>> a=u'really long string containing \u20ac and some other text'
>>> type(a)
>>> a.decode('unicode-escape')
Traceback (most recent call last):
File "", line 1, in
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 30: ordinal not in range(128)