文件score.txt中存储了学生的考试信息,内容如下
小明,98
小刚,90
小红,91
小王,98
小刘,80
请写代码,读取文件数据,并进行如下分析
def read_file(filename):
"""
读取文件数据
:param filename:
:return:
"""
f = open(filename, 'r', encoding='utf-8')
datas = f.readlines()
datas = [item.strip() for item in datas]
f.close()
return datas
def analse_score():
datas = read_file('score.txt')
score_lst = []
for item in datas:
score = int(item.split(',')[1])
score_lst.append(score)
max_score = max(score_lst)
min_score = min(score_lst)
max_score_count = score_lst.count(max_score)
min_score_count = score_lst.count(min_score)
avg = sum(score_lst)/len(score_lst)
print(max_score, min_score)
print(max_score_count, min_score_count)
print(avg)
if __name__ == '__main__':
analse_score()
QQ交流群: 211426309