網站首頁 編程語言 正文
出現這個問題可能是兩個原因造成的:
1、Python2和Python3在字符串編碼上的區別。
2、Python 3.4: str : AttributeError: ‘str’ object has no attribute 'decode
原因一的解決方法:
print (‘張俊’.encode(‘utf-8’). decode(‘utf-8’) ) #必須將字節字符串解碼后才能打印出來
參考鏈接:https://www.cnblogs.com/geekard/archive/2012/10/04/python-string-endec.html
原因二的解決方法:各種編碼方式嘗試解決:utf-8,gbk,ISO-8859-1,gb2312
原因一才是主要原因,主要解決方法。
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‘’)
記憶小技巧
編碼就是encode,把你認識的轉為,機器人認識的
解碼decode,就是吧一堆機器認識的,解釋為人能讀懂的
原文鏈接:https://blog.csdn.net/qq_43192819/article/details/108981008
相關推薦
- 2022-03-15 box-shadow上下左右四個邊框設置陰影樣式
- 2023-11-14 python matlibplot將不同數據的柱狀圖和折線圖畫在同一張圖中
- 2022-04-16 統計C語言二叉樹中葉子結點個數_C 語言
- 2022-12-14 Python如何對音視頻文件進行解析詳解_python
- 2022-12-26 React開發進階redux?saga使用原理詳解_React
- 2022-11-05 Swift?Extension擴展得使用詳細介紹_Swift
- 2022-04-18 Taro 中的 用戶下拉事件,onPullDownRefresh
- 2022-06-27 python中的標準庫html_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支