分享

成功解决TypeError: a bytes-like object is required, not ''str''

 处女座的程序猿 2021-09-28

解决思路

问题出在python3.5和Python2.7在套接字返回值解码上有区别:
python bytes和str两种类型可以通过函数encode()和decode()相互转换,
str→bytesencode()方法。str通过encode()方法可以转换为bytes。
bytes→strdecode()方法。如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。

解决方法

line.strip().split(",")  

改为  

line.decode().strip().split(",")

大功告成!

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多