當前位置:
首頁 >
成功解决TypeError: a bytes-like object is required, not 'str'
發布時間:2025/3/21
24
豆豆
生活随笔
收集整理的這篇文章主要介紹了
成功解决TypeError: a bytes-like object is required, not 'str'
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
成功解決TypeError: a bytes-like object is required, not 'str'
?
目錄
解決問題
解決思路
解決方法
?
?
解決問題
TypeError: a bytes-like object is required, not 'str'
?
?
?
?
解決思路
問題出在python3.5和Python2.7在套接字返回值解碼上有區別:
python bytes和str兩種類型可以通過函數encode()和decode()相互轉換,
str→bytes:encode()方法。str通過encode()方法可以轉換為bytes。
bytes→str:decode()方法。如果我們從網絡或磁盤上讀取了字節流,那么讀到的數據就是bytes。要把bytes變為str,就需要用decode()方法。
?
?
?
解決方法
將
line.strip().split(",") ?
改為 ?
line.decode().strip().split(",")
大功告成!
?
?
總結
以上是生活随笔為你收集整理的成功解决TypeError: a bytes-like object is required, not 'str'的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ML之MaL: 流形学习MaL的概念认知
- 下一篇: 成功解决ModuleNotFoundEr