转 Python-IndexError: list index out of range
https://www.cnblogs.com/2bjiujiu/p/9063864.html
Error:IndexError: list index out of range
Where?
对Python中有序序列进行按索引取值的时候,出现这个异常
Why?
对于有序序列: 字符串 str 、列表 list 、元组 tuple进行按索引取值的时候,默认范围为 0 ~ len(有序序列)-1,计数从0开始,而不是从1开始,最后一位索引则为总长度减去1。当然也可以使用 负数表示从倒数第几个,计数从-1开始,则对于有序序列,总体范围为 -len(有序序列) ~ len(有序序列)-1,如果输入的取值结果不在这个范围内,则报这个错
Way?
检查索引是否在 -len(有序序列) ~ len(有序序列)-1 范围内,修改正确
错误代码:
1
2
3
4
|
name = "beimenchuixue" students = [ "beimenchuixue" , "boKeYuan" , "Python" , "Golang" ] print (name[ 20 ]) print (students[ 4 ]) |
正确代码:
1
2
3
4
|
name = "beimenchuixue" students = [ "beimenchuixue" , "boKeYuan" , "Python" , "Golang" ] print (name[ 3 ]) print (students[ 3 ]) |
##########for 循环 遇到 IndexError:
https://www.jianshu.com/p/f1b58ec12b72
失败原因:爬虫在做xpath时候匹配到空值,粗心大意以为网页很简单,没仔细看,因为标识tr还有空值
解决方法:加上try.....except 错误机制跳过tr空值
try:
#寻找tr下面的标识
qihao = item.select('td strong')[0].text
onewei = item.select('td div')[0].text
twowei = item.select('td div')[1].text
threewei = item.select('td div')[2].text
fourwei = item.select('td div')[3].text
fivewei = item.select('td div')[4].text
sixwei = item.select('td div')[5].text
sevenwei = item.select('td div')[6].text
list = [qihao,onewei,twowei,threewei,fourwei,fivewei,sixwei,sevenwei]
all_lists.append(list)
print(list)
except IndexError:
pass
转 Python-IndexError: list index out of range的更多相关文章
- Python常见错误:IndexError: list index out of range
用python写脚本查询字典时,在遍历字典时循环到某一项时老是报错 出现这种错误有两种情况: 第1种可能情况 list[index]index超出范围 第2种可能情况 list是空值就会出现 In ...
- python报错IndexError: list index out of range
今天写个ping vpn的python脚本,报错IndexError: list index out of range 最后查看是python读取文件中出现空格 去掉空格即可
- pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”
pyinstaller将py打包为exe文件,用pysintaller居然报错 File "c:\anaconda3\lib\site-packages\PyInstaller\depend ...
- IndexError:string index out of range
IndexError:string index out of range 出现在下标越界的情况,如 item[1],可能为空的时候下标就会越界
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...
- mac安装MySQLdb:IndexError: string index out of range
使用mac安装MySQLdb的时候出现string index out of range 大概的错误是这样的: 然后尝试手动安装,我下载了包后,依然出现这个错误. 于是百度了下: https://ww ...
- IndexError: list index out of range Python常见错误
引用超过list最大索引,此错误非常常见,注意列表的元素个数 ----------------------------------------------
- MacOS 安装MysqlDB 问题解决方案( 解决 IndexError: string index out of range)
pip install MySQL-python时报错如下: Command "python setup.py egg_info" failed with error code 1 ...
- IndexError: list index out of range的错误原因
第1种可能情况list[index]index超出范围 第2种可能情况list是一个空的 没有一个元素进行list[0]就会出现该错误 ————————————————版权声明:本文为CSDN博主「m ...
- Mac安装Mysql-python遇到的坑,被这俩报错反复摩擦:'my_config.h' file not found 和 IndexError: string index out of range
最后Stackoverflow上面的大神解决了问题: Link brew install mysql brew unlink mysql brew install mysql-connector-c ...
随机推荐
- VMware Workstation 软件 创建 Ubuntu 14.04虚拟机
VMware Workstation 软件 创建 Ubuntu 14.04虚拟机 1. 安装VMare Workstation 软件 下载VMware 11 软件 http://www.dntk.or ...
- php学习笔记-elseif
<?php if(condition1) { func1(); }elseif(condition2) { func2(); }else { func3(); } ?> elseif需要明 ...
- HiveServer2的配置使用
HiveServer2的配置和使用 hive-site.xml配置 hiveserver2的配置 <property> <name>hive.support.concurren ...
- Luogu 3761 [TJOI2017]城市
BZOJ 4890. 在树上断开一条边之后会形成两个联通块,如果要使这一条边接回去之后保持一棵树的形态,那么必须在两个联通块之间各找一个点连接. 那么,对于每一条可能断开的边,它产生的答案是以下两者的 ...
- Entity Framework Tutorial Basics(34):Table-Valued Function
Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...
- 【Arcgis for android】Error inflating class com.esri.android.map.MapView【已解决】
解决方案:如果你是一个项目之前调试是好的,突然调试报这个错,听我的,直接卸载手机上调试的这个程序,重新调试,你会发现ok了 环境:arcgis android 10.2 错误:E/AndroidRun ...
- Android消息传递之EventBus 3.0
Android消息传递之EventBus 3.0使用详解 http://www.cnblogs.com/whoislcj/p/5595714.html EventBus 3.0进阶:源码及其设计模式 ...
- C++时间操作的汇总
. 获取当前时间 time_t cur_time = time(NULL); . 把整数时间转为字符串时间 string GetStringTime(const time_t time) { stru ...
- Linux之创建777权限的文件
服务器中运行项目的时候,有时候会出现图片上传失败,查看报错原因才知道是文件夹没有写入权限导致上传失败. 方案1: 在使用Linux命令更改对应目录的权限 方案2: 在代码中创建文件夹的时候给予对应的7 ...