1. 程式人生 > >Python報“TypeError:abytes-likeobjectisrequired,not‘str’”解決辦法

Python報“TypeError:abytes-likeobjectisrequired,not‘str’”解決辦法

erro peer error: not 字節 python tis 通過 解決

解決辦法非常的簡單,只需要用上python的bytes和str兩種類型轉換的函數encode()、decode()即可!

str通過encode()方法可以編碼為指定的bytes;

反過來,如果我們從網絡或磁盤上讀取了字節流,那麽讀到的數據就是bytes。要把bytes變為str,就需要用decode()方法;

例:

str = ‘this is test‘

str = str.encode()

Python報“TypeError:abytes-likeobjectisrequired,not‘str’”解決辦法