试图从结果字典中获取平均值

Trying to get an average from a dictionary of results

对于此代码,我试图获得 class 的总体平均值。我会打开文件,然后从字典中删除所有字符和线条,只得到每个学生的分数。然后我尝试获取文件的总和,然后将其除以长度,但出现错误。

average = (sum(file)/len(file))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

代码如下:

    with open('classtwo.csv', 'r', newline='') as csv_file:
        newfile = (str.strip("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z") for str in csv_file if str.strip("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"))
        file = [line.strip() for line in newfile if line.strip()]
        average = (sum(file)/len(file))
        print(average)    

有人知道如何解决这个错误吗?

file 是一个字符串列表。 sum 函数需要一个数字。 要获得平均值,您应该将 file 中的每个条目转换为一个数字:

例如file = [float(x) for x in file]