AttributeError: ‘str’ object has no attribute ‘decode’
一般是因為str的類型本身不是bytes,所以不能解碼
兩個概念:
普通str:可理解的語義
字節流str(bytes)(0101010101,可視化顯示)
兩個語法
Encode: 把普通字符串 轉為 機器可識別的bytes
Decode: 把bytes轉為字符串
兩個差異
python3的str 默認不是bytes,所以不能decode,只能先encode轉為bytes,再decode
python2的str 默認是bytes,所以能decode
一個結論
所以str.decode 本質是bytes類型的str的decode
python3經常出現 AttributeError: ‘str’ object has no attribute ‘decode’
非要這樣玩,只能先encode轉為bytes,再decode
強制轉換忽略錯誤:
bytes.decode(‘’utf-8‘’, ‘’ignore‘’)
常用解決方法:
print (‘張俊’.encode(‘utf-8’). decode(‘utf-8’) ) #必須將字節字符串解碼后才能打印出來
————————————————
版權聲明:本文為CSDN博主「檸 檬沒我萌」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_43192819/article/details/108981008