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

2024-04-20 16:06:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试将字符串编码为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 "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)

Tags: 字符串instring错误asciinotpositioncan
3条回答

尝试:

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',这是正确的。

所以你的问题一定是在某个地方,如果你试图对它做些什么,如果有一个隐式转换正在进行(可能是打印,写入流…)

更进一步说,我们需要看一些代码。

我的+1给玛塔在https://stackoverflow.com/a/10561979/1346705的评论和尼克·克雷格·伍德的演示。您已正确解码字符串。问题在于print命令将Unicode字符串转换为控制台编码,而控制台无法显示该字符串。尝试将字符串写入文件,并使用支持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()

然后您将看到(。・ω・。)ノ

这与终端的编码未设置为UTF-8有关。这是我的终点站

$ 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
(。・ω・。)ノ
>>> 

在我的终端上,这个例子可以与上面的一起工作,但是如果我去掉了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变体的文档,以了解如何使此更改永久化。

相关问题 更多 >