首页 > 编程语言 >Python异常 ValueError的问题详解

Python异常 ValueError的问题详解

时间:2023-04-02 09:03:57浏览次数:41  
标签:... Python demo int 详解 key ValueError

导读 这篇文章主要介绍了Python异常 ValueError的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Python异常 ValueError
ValueError: invalid literal for int() with base 10: '*'

试图将一个与数字无关的类型转化为整数,会抛出该异常。

>>> int("99 years ago.")
Traceback (most recent call last):
  File "", line 1, in 
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 "", line 1, in 
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

试图以二进制模式读取文件时指定编码方式。www.linuxprobe.com

>>> with open("protoc-gen-go", "rb+", encoding="utf-8") as file:
...     data = file.read()
...
Traceback (most recent call last):
  File "", line 1, in 
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'

标签:...,Python,demo,int,详解,key,ValueError
From: https://www.cnblogs.com/linuxprobe19/p/17260678.html

相关文章

  • 爬虫简单python小程序
    importrequestsfrombs4importBeautifulSoupbase_url='your_base_url_here'#根据网站的实际URL模式修改此函数defget_page_url(page_number):returnf'{base_url}?page={page_number}'#根据网站的HTML结构,您可能需要修改以下选择器以获取游戏标题defget_g......
  • python的类继承总结
    参考文档:https://blog.csdn.net/yilulvxing/article/details/85374142总结如下:第一种情况:子类和父类都有__init__(self)方法情况继承研究#如果子类也有init方法,那么调用父类的实例属性必须要在子类的init()方法中有这个super().__init__()【这点尤其要注意】#如果子类也有in......
  • Python内存管理
    Python内存管理的三个阶段:1.引用计数引用计数是Python内存管理的第一道防线。当一个对象被引用时,Python会为其分配一段内存,并将其引用计数设置为1。当对象被多次引用时,其引用计数会逐渐增加。当一个对象不再被引用时,Python将其引用计数减少1。当一个对象的引用计数变为......
  • 【v4l2】Linux多媒体框架 - v4l2 core详解
    简介V4L2是Videoforlinux2的简称,是Linux中关于视频设备的内核驱动框架。常用结构体:定义在include/linux/videodev2.hstructv4l2_requestbuffers//申请帧缓冲,对应命令VIDIOC_REQBUFSstructv4l2_capability//视频设备的功能,对应命令VIDIOC_QUERYCAPstr......
  • 【转载】 解决VSCode运行或调试python文件无反应的问题
    ————————————————版权声明:本文为CSDN博主「姜行者」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/kraisi/article/details/127287966  ====================================================  ......
  • import this Python之禅 20条准则
    运行importthis这句代码后返回的内容importthisTheZenofPython,byTimPetersBeautifulisbetterthanugly.Explicitisbetterthanimplicit.Simpleisbetterthancomplex.Complexisbetterthancomplicated.Flatisbetterthannested.Sparseisbette......
  • python pygame播放音频文件
    pythonpygame播放音频文件 pipinstallpygame  importpygame#Initializepygamepygame.init()#LoadtheMP3filepygame.mixer.music.load("1.mp3")#PlaytheMP3filepygame.mixer.music.play()#WaitfortheMP3filetofinishplayingwhil......
  • 字符串(提高组)详解
    字符串(提高组)详解前提提要:本文为提高组字符串,不涉及到后缀科技等相关算法,请放心食用Part1.字符串Hash题目概要如题,给定\(N\)个字符串(第\(i\)个字符串长度为\(M_i\),字符串内包含数字、大小写字母,大小写敏感),请求出\(N\)个字符串中共有多少个不同的字符串。输入格式......
  • 【入门】Go语言排序算法详解
    1、冒泡排序冒泡排序是一种简单的排序算法,它重复地遍历要排序的列表,比较相邻的两个元素,并在必要时交换它们的位置,直到没有任何一对元素需要交换为止。该算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。packagemainimport"fmt"//我们定义了一个名为`bu......
  • python学习-基础案例
    案例一打印排序好的数据#列表方式lst_name=['林黛玉','薛宝钗','贾元春','贾探春','史湘云']lst_sign=['①','②','③','④','⑤']foriinrange(5):print(lst_sign[i],lst_name[i])print(�......