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 =int(record)
record =int(record)
record =int(record)
record.append(int(record+record+record))
#为计算各科数据分列保存数据在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())
fp_w.write("\n")
#写入最高分
fp_w.write(",".join(["统计",
"最高分",
str(max(zip_list)),
str(max(zip_list)),
str(max(zip_list)),
str(max(zip_list))
]
)
)
fp_w.write("\n")
#写入最低分
fp_w.write(",".join(["统计",
"最低分",
str(min(zip_list)),
str(min(zip_list)),
str(min(zip_list)),
str(min(zip_list))
]
)
)
fp_w.write("\n")
#写入平均分
record_no = len(records)
fp_w.write(",".join(["统计",
"平均分",
str(sum(zip_list)/record_no),
str(sum(zip_list)/record_no),
str(sum(zip_list)/record_no),
str(sum(zip_list)/record_no)
]
)
)
注:这种类型的数据处理其实用numpy或者pandas最好了,不过我估计应该时不允许使用非python自带函数~~
页:
[1]