Python - “ascii"编解码器无法解码字节 [英] Python - 'ascii' codec can't decode byte

查看:77
本文介绍了Python - “ascii"编解码器无法解码字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑.我试图编码,但错误说 can't decode....

<预><代码>>>>"你好".encode("utf8")回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中UnicodeDecodeError: 'ascii' 编解码器无法解码位置 0 中的字节 0xe4:序号不在范围内 (128)

我知道如何避免字符串上带有u"前缀的错误.我只是想知道为什么在调用 encode 时错误是无法解码".Python 在幕后做了什么?

解决方案

"你好".encode('utf-8')

encode 将 unicode 对象转换为 string 对象.但是在这里您已经在 string 对象上调用了它(因为您没有 u).所以python必须先将string转换成unicode对象.所以它相当于

"你好".decode().encode('utf-8')

但解码失败,因为字符串不是有效的 ascii.这就是您收到无法解码的投诉的原因.

I'm really confused. I tried to encode but the error said can't decode....

>>> "你好".encode("utf8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

I know how to avoid the error with "u" prefix on the string. I'm just wondering why the error is "can't decode" when encode was called. What is Python doing under the hood?

解决方案

"你好".encode('utf-8')

encode converts a unicode object to a string object. But here you have invoked it on a string object (because you don't have the u). So python has to convert the string to a unicode object first. So it does the equivalent of

"你好".decode().encode('utf-8')

But the decode fails because the string isn't valid ascii. That's why you get a complaint about not being able to decode.

这篇关于Python - “ascii"编解码器无法解码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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