问题描述
这与终端的编码未设置为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变体的文档,以了解如何使此更改永久生效。
解决方法
我在尝试将字符串编码为UTF-8时遇到一些问题。我已经尝试了很多事情,包括使用string.encode('utf-8')
和unicode(string)
,但是出现错误:
UnicodeDecodeError:’ascii’编解码器无法解码位置1的字节0xef:序数不在范围内(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)