python错误提示TypeError: a bytes-like object is required, not str

[python] 2024-04-29 圈点725

摘要:python错误提示TypeError: a bytes-like object is required, not 'str' ; 提示此错误是因为取得的是bytes,而没有decode()进行转化。

python错误提示TypeError: a bytes-like object is required, not 'str' ; 


问题出在python3.5和Python2.7在套接字返回值解码上有区别:

先介绍一下 python bytes和str两种类型转换的函数encode(),decode()

str通过encode()方法可以编码为指定的bytes

反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。

要把bytes变为str,就需要用decode()方法.


提示此错误是因为取得的是bytes,而没有decode()进行转化。



python  

感谢反馈,已提交成功,审核后即会显示