建议以后提问,贴代码和数据时用代码框,就算不用代码框也不要用图片,我觉得如果你贴的时文本的话,应该早就有人来回答了~~with open("score1.txt","r") as fp_r:
#第一行
header = fp_r.readline().strip()+",总成绩"
#读取数据保存在records中
records =[]
record=fp_r.readline()
while record:
records.append(record.split(","))
record=fp_r.readline()
#分数转化为int型,并计算个人总成绩
for record in records:
record[2] =int(record[2])
record[3] =int(record[3])
record[4] =int(record[4])
record.append(int(record[2]+record[3]+record[4]))
#为计算各科数据分列保存数据在zip_list中
zip_list = list(zip(*records))
with open("score2.txt","w") as fp_w:
#写入第一行
fp_w.write(header)
fp_w.write("\n")
#写入每个学生数据
for record in records:
fp_w.write(",".join([str(item) for item in record]))
fp_w.write("\n")
#写入最高分
fp_w.write(",".join(["统计",
"最高分",
str(max(zip_list[2])),
str(max(zip_list[3])),
str(max(zip_list[4])),
str(max(zip_list[5]))
]
)
)
fp_w.write("\n")
#写入最低分
fp_w.write(",".join(["统计",
"最低分",
str(min(zip_list[2])),
str(min(zip_list[3])),
str(min(zip_list[4])),
str(min(zip_list[5]))
]
)
)
fp_w.write("\n")
#写入平均分
record_no = len(records)
fp_w.write(",".join(["统计",
"平均分",
str(sum(zip_list[2])/record_no),
str(sum(zip_list[3])/record_no),
str(sum(zip_list[4])/record_no),
str(sum(zip_list[5])/record_no)
]
)
)
注:这种类型的数据处理其实用numpy或者pandas最好了,不过我估计应该时不允许使用非python自带函数~~ |