Python 从Csv呼叫时,如何计算同一班级中一个人的平均值

Python 从Csv呼叫时,如何计算同一班级中一个人的平均值,python,csv,Python,Csv,我在csv中保存了一个带有名称的分数。我想创建一个平均分数出来的人的名字谁是相同的,并显示这一点 score2 = str(score)#Converts the integer into the string so can be saved fsav = (fulnam + " : " + score2)#Sets the variable as Full name of the user and the score filsav = clsnam + ".csv" #Creates th

我在csv中保存了一个带有名称的分数。我想创建一个平均分数出来的人的名字谁是相同的,并显示这一点

score2 = str(score)#Converts the integer into the string so can be saved
fsav = (fulnam + " : " + score2)#Sets the variable as Full name of the user and the score  
filsav = clsnam + ".csv" #Creates the file that the scores will be saved in
file = open(filsav , "a") #Opens the file so it can be appended
file.write(fulnam + " , ") #Writes the persons name to the
file.write(score2) #Writes the persons score to the file 
file.write("\n")#Moves the file onto a new line
file.close() #Closes and saves the file

显然,您必须读入整个文件才能执行此操作:

import csv

def compute_average(clsnam, fulnam):
    """ Determine average score in class for named person.
    """
    scores = []
    with open(clsnam + ".csv", 'r') as file:
        for name, score in csv.reader(file):
            if name.strip() == fulnam:
                scores.append(int(score))

    return None if not scores else float(sum(scores)) / len(scores)

您是否有我们可以解决的具体问题?如何打印结果?调用函数的结果可以显示为
print(compute\u average('Biology101','Mary Jones'))