解析ascii编解码器无法解码位于序列中的字节而不在范围内

时间:2016-02-21 00:52:17

标签: python python-2.7 unicode encoding decoding

我已经看过所有其他帖子并做了相当多的研究,但我仍然在摸不着头脑。

问题在于:

$ 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。

1 个答案:

答案 0 :(得分:5)

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')