简体   繁体   English

Python错误:“ IndexError:列表索引超出范围”

[英]Error in Python: “IndexError: list index out of range”

I am trying to make a list out of data from an excel file in python, but I receive this whenever I run my code 我正在尝试从python中的excel文件中的数据中列出一个列表,但是每当我运行我的代码时,我都会收到此列表

row[1] = int(row[1])
IndexError: list index out of range
>>> 

This is the code I have that sorts it (by minimum, maximum, and average) 这是我排序的代码(按最小,最大和平均)

f = open("Class 2.csv", "r")
csvread = csv.reader(f)
nlist = []
for row in csvread:
    filter(lambda x: 3 > 0, row)
    row[0] = int(row[0])
    row[1] = int(row[1])
    row[2] = int(row[2])
    row[3] = int(row[3])
    minimum = min(row[1:4])
    row.append(minimum)
    maximum = max(row[1:4])
    row.append(maximum)
    average = round(sum(row[1:4])/3)
    row.append(average)
    nlist.append(row[0:4])
print(nlist)

Row[0] in my excel file is a name as well, so I also get an error that tells me that int(row[0]) cannot work because I is not an integer. 我的excel文件中的Row [0]也是一个名称,所以我也收到一条错误消息,告诉我int(row [0])无法工作,因为我不是整数。 I don't know how to change it so that I don't get this error. 我不知道如何更改它,这样我就不会收到此错误。

Iteration row has no second element. 迭代没有第二个元素。 Check it before sending: 发送前检查一下:

If all values in list are numbers, use map for str to int values: 如果列表中的所有值都是数字,请使用map将str转换为int值:

int_values = map(int, str_list)

or 要么

int_values = map(int, str_list[0:4])

But this will not solve your problem, because in the list of values. 但这不会解决您的问题,因为在值列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM