ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-29 11:09:55  阅读:232  来源: 互联网

标签:decoding python python-2-7 unicode encoding


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

这是问题所在:

$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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有