UnicodeDecodeError,无效的继续字节 [英] UnicodeDecodeError, invalid continuation byte

查看:108
本文介绍了UnicodeDecodeError,无效的继续字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下项目失败?为什么它以 latin-1成功?

Why is the below item failing? Why does it succeed with "latin-1" codec?

o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
v = o.decode("utf-8")

将导致以下结果:

 Traceback (most recent call last):  
 File "<stdin>", line 1, in <module>  
 File "C:\Python27\lib\encodings\utf_8.py",
 line 16, in decode
     return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
 'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte


推荐答案

在二进制文件中,0xE9类似于 1110 1001 。如果您在Wikipedia上阅读有关 UTF-8 的内容,则会看到这样的内容字节后必须是两个格式为 10xx xxxx 的字节。因此,例如:

In binary, 0xE9 looks like 1110 1001. If you read about UTF-8 on Wikipedia, you’ll see that such a byte must be followed by two of the form 10xx xxxx. So, for example:

>>> b'\xe9\x80\x80'.decode('utf-8')
u'\u9000'

但这只是造成异常的机械原因。在这种情况下,您几乎可以肯定用拉丁文1编码了一个字符串。您可以看到UTF-8和拉丁文1看起来如何不同:

But that’s just the mechanical cause of the exception. In this case, you have a string that is almost certainly encoded in latin 1. You can see how UTF-8 and latin 1 look different:

>>> u'\xe9'.encode('utf-8')
b'\xc3\xa9'
>>> u'\xe9'.encode('latin-1')
b'\xe9'

(注意,我在这里混合使用Python 2和3表示形式。输入在任何版本的Python中均有效,但是您的Python解释器不太可能以这种方式同时显示unicode和字节字符串。)

(Note, I'm using a mix of Python 2 and 3 representation here. The input is valid in any version of Python, but your Python interpreter is unlikely to actually show both unicode and byte strings in this way.)

这篇关于UnicodeDecodeError,无效的继续字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆