【Bug-python】IndexError: list index out of range
来源:互联网 发布:彩通网络小额贷款牌照 编辑:程序博客网 时间:2024/06/06 16:43
报错: IndexError: list index out of range
错误提示:
代码如下:
with open("linux_Yue_01.txt","r") as testFile:
testfileList =[splitFileNameAndLabel(filename = v)for v testFile.read().split("\n")]
图示如下:
错误分析:
”IndexError: list index out of range”这种错误一般有两种情况:
第一种可能情况:
list[index]index超出范围
第二种可能情况:
list是一个空的,没有一个元素
进行list[0]就会报该错误。
一般外部输入的数据都可能存在问题,所以通常在readlines或者read后要做一次处理。
这样就避免了空行,字段数不足,以及类型转换出错。
解决方案:
针对第一种情况,通过调试检查代码,不多说了。
关于第二种情况,有两种方法:
第一种:检查读入的数据是都有问题,比如读入的txt文件是否存在空行等等,但本人更提倡第二种解决方案,在代码上改错。
with open("linux_Yue_01.txt","r") as testFile:
testfileList = []
for v testFile.read().split("\n")
if not v.strip(): #字符串去掉空格不为空
continue
testfileList.extend([splitFileNameAndLabel(v)])
注意:[splitFileNameAndLabel(v)]是又方括号的,否则不能成为列表中嵌套列表了
图示如下:
- 【Bug-python】IndexError: list index out of range
- Python:IndexError: list index out of range
- **Python错误记录:IndexError: list index out of range**
- python IndexError: list assignment index out of range
- IndexError: list index out of range
- IndexError: list index out of range的错误原因
- IndexError: list index out of range列表索引超出范围
- Python 爬虫 raise self._value IndexError: list index out of range
- python PIL, 错误:IndexError: string index out of range
- python 脚本IndexError: tuple index out of range
- IndexError: tuple index out of range
- list不能修改不存在的项 IndexError: list assignment index out of range
- int(sys.argv[1]) IndexError: list index out of range报错的原因
- caffe 绘制acceracy曲线 IndexError: list index out of range的解决方案
- list index out of range
- list index out of range
- caffe 根据训练输出的log 绘制accuracy loss 曲线时遇到 IndexError:list index out of range
- 每日一坑 pyinstaller 将.py生成.exe 报错 “IndexError: tuple index out of range”
- ZCMU-1827-区间k大数查询
- 安装Nginx教程[window和linux下]
- 7 信号通信2
- 时间转换处理工具类
- Linux总结分析
- 【Bug-python】IndexError: list index out of range
- 2016年末日常点滴总结和近期阅读书单
- JAVA之sort()排序特例
- Android音视频学习第6章:视频直播实现完整代码
- 模板:policy类的简单使用
- 配置Eclipse编写HTML/JS/CSS/JSP页面的自动提示。
- 静态链接库和动态链接库的区别及优缺点
- PLSQL32位免安装版客户端配置
- 88w8686 wifi模块的linux设备驱动的测试