網站首頁 編程語言 正文
Python異常?ValueError
ValueError: invalid literal for int() with base 10: '*'
試圖將一個與數字無關的類型轉化為整數,會拋出該異常。
>>> int("99 years ago.")
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '99 years ago.'
規避方法:int函數參數應該合法使用。int函數使用傳送門:Python中的int函數使用
ValueError: too many values to unpack (expected 2)
試圖遍歷字典時同時遍歷鍵和值。
>>> demo = {"China": "Beijing", "Japan": "Tokyo"}
>>> for k, v in demo:
... ? ? print(k, v)
...
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
Python只允許對字典key的遍歷,因此上面的遍歷方式是錯誤的。
規避方法
方法一:使用dict[key]的方式同時獲取value
>>> demo = {"China": "Beijing", "Japan": "Tokyo"}
>>> for key in demo:
... ? ? print(key, demo[key])
...
China Beijing
Japan Tokyo
方法二:使用items方法
>>> demo = {"China": "Beijing", "Japan": "Tokyo", "the United States": "Washington D.C."}
>>> for key, value in demo.items():
... ? ? print(key, value)
...
China Beijing
Japan Tokyo
the United States Washington D.C.
ValueError: binary mode doesn't take an encoding argument
試圖以二進制模式讀取文件時指定編碼方式。
>>> with open("protoc-gen-go", "rb+", encoding="utf-8") as file:
... ? ? data = file.read()
...
Traceback (most recent call last):
? File "<stdin>", line 1, in <module>
ValueError: binary mode doesn't take an encoding argument
規避方法:避免使用encoding關鍵字
>>> with open("protoc-gen-go", "rb+") as file:
... ? ? data = file.read(10)
...
>>> data
b'\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00'
原文鏈接:https://blog.csdn.net/TCatTime/article/details/88085292
相關推薦
- 2022-10-31 關于命令行執行Python腳本的傳參方式_python
- 2022-04-08 swift實現簡易計算器項目_Swift
- 2022-11-05 解決使用pip安裝報錯:Microsoft?Visual?C++?14.0?is?required.
- 2022-07-26 更新kali源并,安裝dirseach
- 2023-01-29 python如何利用cv2.rectangle()繪制矩形框_python
- 2023-12-23 React環境安裝配置
- 2022-07-21 css讓不定寬高的div,垂直水平居中
- 2022-05-16 Qt5中QML自定義環形菜單/環形選擇框的實現_C 語言
- 最近更新
-
- 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同步修改后的遠程分支