在python中读取文件时出现CSV错误

在python中读取文件时出现CSV错误,python,python-3.x,csv,Python,Python 3.x,Csv,我试图用python读取CSV文件,但在第二次排序操作中出现错误。我对这一点完全不熟悉,有什么想法我哪里出了问题吗 import csv file_var = open('class.csv') csv_file = csv.reader(file_var) newlist = [] for row in csv_file: row[1] = int(row[1]) #Convert the string values into integers row[2] = int(r

我试图用python读取CSV文件,但在第二次排序操作中出现错误。我对这一点完全不熟悉,有什么想法我哪里出了问题吗

import csv

file_var = open('class.csv')
csv_file = csv.reader(file_var)
newlist = []

for row in csv_file:
    row[1] = int(row[1]) #Convert the string values into integers
    row[2] = int(row[2])
    row[3] = int(row[3])
    minimum = min(row[1:3]) #Finds the lowest score for each student
    row.append(minimum) #Adds it to the list
    maximum = max(row[1:3]) #Finds the highest score for each student
    row.append(maximum) #Adds it to the list
    average = round(sum(row[1:3])/3) #Calculate the average of the 3 scores and rounds to nearest whole no.
    row.append(average) #Adding it to a list
    newlist.append(row[0:3]) #adding each row to the newlist - [0:4] the range of items on each row

lowscore = [[x[2], x[0]] for x in newlist] #For sorting lowest scores
print('\nLowest scores\n')
for entry in sorted(lowscore): #iterate through lowscore and sort based on [0]
    print(entry)

highscore = [[x[2], x[0]] for x in newlist]
print('\nHighest scores\n')
for entry in sorted(maximum): #iterate through highscore and sort based on [0]
    print(entry)

avgscore = [[x[2], x[0]] for x in newlist]
print('\nAverage scores\n')
for entry in sorted(average): #iterate through average and sort based on [0]
    print(entry)

代码中第26行的“TypeError:'int'对象不可编辑”。
maximum
在此定义:
maximum=max(行[1:3])
。这是一个数字,不是列表。这就是为什么。原谅我的知识不足-我该如何纠正?你被原谅了:P
low score
high score
avgscore
都做同样的事情。它们都引用了新列表中x的
[[x[2],x[0]]
。您可能会执行
排序(newlist,key=lambda x:x[您希望排序的列])
以便按最小值排序(如果是
newlist[4]
),您可以执行
排序(newlist,key=lambda x:x[4])
并在循环中:
对排序中的条目进行排序(newlist,key=lambda x:x[4]):打印条目
。您可以添加
reverse=True
sorted()
到,您猜到了,反转排序顺序:
sorted(newlist,key=lambda x:x[4],reverse=True)
“TypeError:'int'object not iterable”用于代码中的第26行。
这里定义了最大值:
max=max(行)[1:3])
。这是一个数字,而不是一个列表。这就是为什么。请原谅我的知识不足-我该如何纠正这一点?你被原谅了:P
low score
high score
avgscore
都做了同样的事情。它们都引用了
[[x[2],x[0]]来表示新列表中的x
。你可能会做
排序(newlist,key=lambda x:x)[column\u you\u to \u sort by]
因此要按最小值排序(如果是
newlist[4]
),您可以执行
排序(newlist,key=lambda x:x[4])
并在循环中:
对于排序中的条目(newlist,key=lambda x:x[4]):打印条目
。您可以将
reverse=True
添加到
排序()
to,您猜对了,颠倒排序顺序:
sorted(newlist,key=lambda x:x[4],reverse=True)