UnicodeDecodeError: 'ascii' 编解码器无法解码位置 1 处的字节 0xef。

20 浏览
0 Comments

UnicodeDecodeError: 'ascii' 编解码器无法解码位置 1 处的字节 0xef。

我在尝试将一个字符串编码为UTF-8时遇到了一些问题。我尝试了许多方法,包括使用 string.encode('utf-8')unicode(string),但是我遇到了以下错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1: ordinal not in range(128)

这是我的字符串:

(。・ω・。)ノ

我不明白出了什么问题,有什么想法吗?

编辑:问题是以原样打印字符串时无法正确显示。另外,我尝试转换时也出现了这个错误:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)

0