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

我有几个问题试图将string编码为UTF-8。 我已经尝试了很多东西,包括使用string.encode('utf-8')unicode(string) ,但我得到的错误:

UnicodeDecodeError:'ascii'编解码器无法解码位置1中的字节0xef:序号不在范围内(128)

这是我的string:

 (。・ω・。)ノ 

我不明白发生什么事了,有什么想法?

编辑:问题是,打印string,因为它不能正常显示。 此外,当我尝试将其转换为此错误:

 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 "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128) 

这是与您的terminal编码不被设置为UTF-8。 这是我的terminal

 $ echo $LANG en_GB.UTF-8 $ python Python 2.7.3 (default, Apr 20 2012, 22:39:59) [GCC 4.6.3] 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 (。・ω・。)ノ>>> 

在我的terminal的例子与上述工作,但如果我摆脱了LANG设置,那么它将无法正常工作

 $ unset LANG $ python Python 2.7.3 (default, Apr 20 2012, 22:39:59) [GCC 4.6.3] 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 "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128) >>> 

请参阅您的linux变体的文档,以发现如何使这个变化永久。

尝试:

 string.decode('utf-8') # or: unicode(string, 'utf-8') 

编辑:

'(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'.decode('utf-8')u'(\uff61\uff65\u03c9\uff65\uff61)\uff89' ,这是正确的。

所以你的问题必须在某个地方,可能如果你试图做一些事情,是有一个隐式的转换正在进行(可能是打印,写入stream…)

说更多,我们将需要看到一些代码。

我的+1对马塔的评论在https://stackoverflow.com/a/10561979/1346705和尼克·克雷格伍德的示范。; 您已经正确解码了string。 问题是print命令,因为它将Unicodestring转换为控制台编码,控制台不能显示string。 尝试将string写入一个文件,并使用一些支持Unicode的体面编辑器来查看结果:

 import codecs s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89' s1 = s.decode('utf-8') f = codecs.open('out.txt', 'w', encoding='utf-8') f.write(s1) f.close() 

然后你会看到(。・ω・。)ノ

如果您正在使用远程主机,请查看本地 PC上的/etc/ssh/ssh_config

当这个文件包含一行时:

 SendEnv LANG LC_* 

在行首添加#注释它。 这可能有帮助。

通过这一行, ssh将与PC相关的语言环境variables发送到远程主机。 它造成了很多问题。

没有问题,我的terminal。 上面的答案帮助我find了正确的方向,但直到我添加'ignore'之前,它才起作用。

 fix_encoding = lambda s: s.decode('utf8', 'ignore') 

正如下面的评论所指出的,这可能会导致不希望的结果。 OTOH它也可能只是做的伎俩足以让事情工作,你不关心失去一些字符。

它看起来像你的string编码为utf-8 ,那么究竟是什么问题? 或者你想在这里做什么..?

 Python 2.7.3 (default, Apr 20 2012, 22:39:59) [GCC 4.6.3] 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 (。・ω・。)ノ>>> s2 = u'(。・ω・。)ノ' >>> s2 == s1 True >>> s2 u'(\uff61\uff65\u03c9\uff65\uff61)\uff89' 

这适用于Ubuntu 15.10:

 sudo locale-gen "en_US.UTF-8" sudo dpkg-reconfigure locales 

在我的情况下,这是由我的Unicode文件与“BOM”保存。 为了解决这个问题,我使用BBEdit打开文件,并做了一个“另存为…”select编码“Unicode(UTF-8)”,而不是它带来的是“Unicode(UTF-8,带有BOM) “

我得到了相同types的错误,我发现控制台不能显示另一种语言的string。 因此,我做了下面的代码更改将default_charset设置为UTF-8。

 data_head = [('\x81\xa1\x8fo\x89\xef\x82\xa2\x95\xdb\x8f\xd8\x90\xa7\x93x\x81\xcb3\x8c\x8e\x8cp\x91\xb1\x92\x86(\x81\x86\x81\xde\x81\x85)\x81\xa1\x8f\x89\x89\xf1\x88\xc8\x8aO\x81A\x82\xa8\x8b\xe0\x82\xcc\x90S\x94z\x82\xcd\x88\xea\x90\xd8\x95s\x97v\x81\xa1\x83}\x83b\x83v\x82\xcc\x82\xa8\x8e\x8e\x82\xb5\x95\xdb\x8c\xaf\x82\xc5\x8fo\x89\xef\x82\xa2\x8am\x92\xe8\x81\xa1', 'shift_jis')] default_charset = 'UTF-8' #can also try 'ascii' or other unicode type print ''.join([ unicode(lin[0], lin[1] or default_charset) for lin in data_head ]) 

这是最好的答案: https : //stackoverflow.com/a/4027726/2159089

在linux中:

 export PYTHONIOENCODING=utf-8 

所以sys.stdout.encoding是可以的。

尝试在脚本开始处将系统默认编码设置为utf-8 ,以便所有string都使用该编码进行编码。

 import sys reload(sys) sys.setdefaultencoding('utf-8') 

我用'ENGINE'解决了在settings.py文件中改变的问题:'django.db.backends.mysql',不要使用'ENGINE':'mysql.connector.django',

只需使用str()将文本显式转换为string即可。 为我工作。