解决 ascii codec can't decode byte in position ordinal not in range
Resolving ascii codec can't decode byte in position ordinal not in range
我已经看过所有其他帖子并做了很多研究,但我仍然摸不着头脑。
这是问题所在:
$ 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)
>>>
无论我怎样尝试,错误仍然存在。
我想问题可能很简单。如果有人能帮助解释发生了什么,以及如何解决这个问题,我将不胜感激。
我 python 2.7 ubuntu。
b
编码为 UTF-8,因此您必须 .decode
将其编码为 Unicode。
print a + b.decode('utf-8')
在 Python 2.7.6 中测试 Ubuntu。
如果你想在 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')
我已经看过所有其他帖子并做了很多研究,但我仍然摸不着头脑。
这是问题所在:
$ 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)
>>>
无论我怎样尝试,错误仍然存在。
我想问题可能很简单。如果有人能帮助解释发生了什么,以及如何解决这个问题,我将不胜感激。
我 python 2.7 ubuntu。
b
编码为 UTF-8,因此您必须 .decode
将其编码为 Unicode。
print a + b.decode('utf-8')
在 Python 2.7.6 中测试 Ubuntu。
如果你想在 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')