UnicodeDecodeError:"ascii"编解码器无法解码位置1的字节0xef [英] UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1
问题描述
我在尝试将字符串编码为UTF-8时遇到一些问题.我已经尝试了很多事情,包括使用string.encode('utf-8')
和unicode(string)
,但是出现错误:
I'm having a few issues trying to encode a string to UTF-8. I've tried numerous things, including using string.encode('utf-8')
and unicode(string)
, but I get the error:
UnicodeDecodeError:'ascii'编解码器无法解码位置1的字节0xef:序数不在范围内(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1: ordinal not in range(128)
这是我的字符串:
(。・ω・。)ノ
我不知道出了什么问题,知道吗?
I don't see what's going wrong, any idea?
问题是按原样打印字符串不能正确显示.另外,当我尝试将其转换时出现此错误:
The problem is that printing the string as it is does not show properly. Also, this error when I try to convert it:
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)
推荐答案
这与未将终端的编码设置为UTF-8有关.这是我的终端机
This is to do with the encoding of your terminal not being set to UTF-8. Here is my 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
(。・ω・。)ノ
>>>
在我的终端上,该示例适用于上面的示例,但是如果我放弃了LANG
设置,那么它将无法正常工作
On my terminal the example works with the above, but if I get rid of the LANG
setting then it won't work
$ 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变体版本的文档,以了解如何使此更改永久生效.
Consult the docs for your linux variant to discover how to make this change permanent.
这篇关于UnicodeDecodeError:"ascii"编解码器无法解码位置1的字节0xef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!