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 = '(xefxbdxa1xefxbdxa5xcfx89xefxbdxa5xefxbdxa1)xefxbex89'
>>> 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 = '(xefxbdxa1xefxbdxa5xcfx89xefxbdxa5xefxbdxa1)xefxbex89'
>>> 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 = '(xefxbdxa1xefxbdxa5xcfx89xefxbdxa5xefxbdxa1)xefxbex89'
>>> 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屋!