IndexError: list index out of range - 报错原因分析 (文件中有空行)

在今晚练习Matplotlib读取CSV文件并绘图的过程中,出现错误:

IndexError: list index out of range

百思不得其解,通过百度相关答案查找原因。

这个错误出现大约有两种情况:
第1种可能情况
list[index]index超出范围
第2种可能情况
list是一个空的 没有一个元素
进行list[0]就会出现该错误

我的错误根因为第2种,文件中有空行,删除并重新Run一下,问题解决。

Traceback (most recent call last):
  File "/Users/Rayinitial/PycharmProjects/Work/Matplotlib.py", line 141, in <module>
    x.append(int(row[0]))
IndexError: list index out of range
['1', '5']
['2', '3']
['3', '4']
['4', '7']
['5', '4']
['6', '3']
['7', '5']
['8', '7']
['9', '4']
['10', '4']
[]

来源参考: https://blog.csdn.net/zhoubbt/article/details/53673637



作者:Rayinitial
链接:https://www.jianshu.com/p/475d97c4a09f
来源:简书
 

发布了60 篇原创文章 · 获赞 42 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/u011159607/article/details/95209783
今日推荐