编程语言
首页 > 编程语言> > python – 解析ascii编解码器无法解码位于序列中的字节而不在范围内

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

作者:互联网

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

这是问题所在:

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

解决方法:

b编码为UTF-8,因此您必须将其解码为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')

标签:decoding,python,python-2-7,unicode,encoding
来源: https://codeday.me/bug/20190829/1760050.html