for line in fr: line=line.replace("\n","") ##去掉换行符 person=line.split(",") ##变成列表 scores.append(person) ##变成二维数组 fr.close() isFirst=True for person in scores: if isFirst: person.append("sum") ##在第一行添加一个"sum" isFirst=False else: s=0 for mark in person[3:]: ##读取成绩 s+=float(mark) person.append("{:.2f}".format(s)) ##添加成绩 print(scores) fw=open("result.csv","w") for person in scores: for e in person: print(e,",",sep="",end="",file=fw) ##写入每行的数据 print(file=fw) ##换行 fw.close()
##找出最大最小值 count=0 isFirst=True for person in scores: if isFirst: isFirst=False else: mark=float(person[5]) sSum+=mark if mark>=sMax: sMax=mark if mark<=sMin: sMin=mark mark=float(person[4]) sjSum+=mark if mark>=sjMax: sjMax=mark if mark<=sjMin: sjMin=mark mark=float(person[3]) ojSum+=mark if mark>=ojMax: ojMax=mark if mark<=ojMin: ojMin=mark
##将符合条件的人员添加到对应的列表中 isFirst=True for person in scores: if isFirst: isFirst=False else: count+=1 print(person) mark=float(person[5]) if mark==sMax: sMax1.append(person) if mark==sMin: sMin1.append(person) mark=float(person[4]) if mark==sjMax: sjMax1.append(person) if mark==sjMin: sjMin1.append(person) mark=float(person[3]) if mark==ojMax: ojMax1.append(person) if mark==ojMin: ojMin1.append(person)
##将结果写入一个新的csv文件 fw=open("汇总.csv","w") for i in sMax1: print("总分最高",",",i[0],",",i[1],",",i[5],file=fw) for i in sMin1: print("总分最低",",",i[0],",",i[1],",",i[5],file=fw) for i in sjMax1: print("主观分最高",",",i[0],",",i[1],",",i[4],file=fw) for i in sjMin1: print("主观分最低",",",i[0],",",i[1],",",i[4],file=fw) for i in ojMax1: print("客观分最高",",",i[0],",",i[1],",",i[3],file=fw) for i in ojMin1: print("客观分最低",",",i[0],",",i[1],",",i[3],file=fw) print("总分分均值",",","{:.2f}".format(sAvg),file=fw) print("主观分均值",",","{:.2f}".format(sjAvg),file=fw) print("客观分均值",",","{:.2f}".format(ojAvg),file=fw) fw.close()