Python異常?ValueError的問題
Python異常 ValueError
ValueError: invalid literal for int() with base 10: '*'
試圖將一個與數(shù)字無關(guān)的類型轉(zhuǎn)化為整數(shù),會拋出該異常。
>>> 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.'
規(guī)避方法:int函數(shù)參數(shù)應(yīng)該合法使用。int函數(shù)使用傳送門:Python中的int函數(shù)使用
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的遍歷,因此上面的遍歷方式是錯誤的。
規(guī)避方法
方法一:使用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
試圖以二進(jìn)制模式讀取文件時指定編碼方式。
>>> 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
規(guī)避方法:避免使用encoding關(guān)鍵字
>>> with open("protoc-gen-go", "rb+") as file: ... ? ? data = file.read(10) ... >>> data b'\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00'
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python ValueError: invalid literal for int() with base 10 實用解決方法
- 解決Python報錯:ValueError:operands?could?not?be?broadcast?together?with?shapes
- 解決Python報錯Valueerror: Expected 2d Array Got 1d Array Instead
- Python中ValueError報錯的原因和解決辦法
- Python報錯ValueError: cannot reindex from a duplicate axis的解決方法
- Python報錯ValueError:?cannot?convert?float?NaN?to?integer的解決方法
- 解決Python報錯ValueError list.remove(x) x not in list問題
- Python中異常類型ValueError使用方法與場景
- Python ValueError: all input arrays must have the same shap的問題解決
相關(guān)文章
Django csrf 兩種方法設(shè)置form的實例
今天小編就為大家分享一篇Django csrf 兩種方法設(shè)置form的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02一文教你如何創(chuàng)建Python虛擬環(huán)境venv
創(chuàng)建?Python?虛擬環(huán)境是一個很好的實踐,可以幫助我們管理項目的依賴項,避免不同項目之間的沖突,下面就跟隨小編一起學(xué)習(xí)一下如何創(chuàng)建Python虛擬環(huán)境venv吧2024-12-12Python給Excel寫入數(shù)據(jù)的四種方法小結(jié)
本文主要介紹了Python給Excel寫入數(shù)據(jù)的四種方法小結(jié),包含openpyxl庫、xlsxwriter庫、pandas庫和win32com庫,具有一定的參考價值,感興趣的可以了解一下2025-02-02pycharm沒有找到manage?repositories按鈕的解決辦法
這篇文章主要給大家介紹了關(guān)于pycharm沒有找到manage?repositories按鈕的解決辦法,pycharm是用來寫python的可視化代碼軟件,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07使用Python和PySpark進(jìn)行數(shù)據(jù)分析的實戰(zhàn)教程
數(shù)據(jù)分析是當(dāng)今信息時代中至關(guān)重要的技能之一,Python和PySpark作為強(qiáng)大的工具,提供了豐富的庫和功能,使得數(shù)據(jù)分析變得更加高效和靈活,在這篇文章中,我們將深入探討如何使用Python和PySpark進(jìn)行數(shù)據(jù)分析,需要的朋友可以參考下2024-01-01python 實現(xiàn)圖片修復(fù)(可用于去水?。?/a>
這篇文章主要介紹了python 實現(xiàn)圖片修復(fù)(可用于去水?。瑤椭蠹腋玫睦斫夂褪褂胦pencv庫,感興趣的朋友可以了解下2020-11-11