【问题标题】:Resolving ascii codec can't decode byte in position ordinal not in range解决 ascii 编解码器无法解码位置序数不在范围内的字节
【发布时间】:2016-02-21 00:52:16
【问题描述】:

我已经看过所有其他帖子并进行了大量研究,但我仍然摸不着头脑。

问题来了:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=u'My Mate\u2019s'
>>> b='\xe2\x80\x99s BBQ'
>>> print a
My Mate’s
>>> print b
’s BBQ

因此,变量本身被精细打印,但打印的是串联:

>>> print a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

给出一个解码错误。所以,我尝试解码字符串:

>>> print a.decode('utf-8')+b.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 7: ordinal not in range(128)

错误变为编码错误。所以,我尝试了几种方法来通知编码:

>>> print a.decode('utf-8').encode('utf-8')+b.decode('utf-8').encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 7: ordinal not in range(128)
>>> print a.decode('ascii','ignore')+b.decode('ascii','ignore')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 7: ordinal not in range(128)
>>> print a.decode('utf-8').encode('ascii','ignore') +b.decode('utf-8').encode('ascii','ignore')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 7: ordinal not in range(128)
>>> 

无论我尝试什么,错误仍然存​​在。

我想问题可能很简单。我会很感激有人帮助解释发生了什么,以及如何解决这个问题。

我在 ubuntu 上有 python 2.7。

【问题讨论】:

    标签: python python-2.7 unicode encoding decoding


    【解决方案1】:

    b 编码为 UTF-8,因此您必须将 .decode 编码为 Unicode。

    print a + b.decode('utf-8')
    

    在 Ubuntu 上用 Python 2.7.6 测试。

    如果你想要两者都使用 UTF-8,你可以这样做:

    print a.encode('utf-8') + b
    

    我会解释为什么你的每一次尝试都不起作用:

    a + b # the default decoding is ascii which cannot decode UTF-8
    a.decode('utf-8')+b.decode('utf-8') # you don't need to decode Unicode
    

    同样,您不需要解码 Unicode。

    a.decode('utf-8').encode('utf-8')+b.decode('utf-8').encode('utf-8')
    

    您一直在尝试解码 Unicode。您应该做的是对其进行编码或解码b。

    a.decode('ascii','ignore')+b.decode('ascii','ignore')
    

    最后你再次尝试解码 Unicode。这里要指出的是,UTF-8 是一种编码。您从 UTF-8 解码为 Unicode。

    a.decode('utf-8').encode('ascii','ignore') +b.decode('utf-8').encode('ascii','ignore')
    

    【讨论】:

    • 啊。灯泡点亮。谢谢!
    • 干杯,伙计。祝您编码愉快。
    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2011-05-13
    • 2014-02-19
    • 2018-07-26
    • 2020-11-06
    • 2014-08-12
    • 2017-03-30
    • 2018-05-13
    相关资源
    最近更新 更多