题目:
输入文件:
- 三列:学号、姓名成绩。
- 列之间用逗号分割,比如“101,小张,88”。
- 行之间用\n换行分割。
输出:最高分、最低分、平均分。
解题思路:
1、读取文件,遍历每一行。
2、处理数据,将关键数据添加至新的列表。
3、输出计算后的数据。
结果:
def Path_score():
score = []
with open("E:\python_crawler\每日一题\dx.txt","r",encoding="utf-8") as f:
for line in f:
line = line[:-1]
line = line.split(",")
line = int(line[2])
score.append(line)
max_score = max(score)
min_score = min(score)
average_score = sum(score) /len(score)
return max_score,min_score,average_score
max_score,min_score,average_score = Path_score()
print(f"max_score:{max_score}分,min_score:{min_score}分,average_score:{average_score}分")
---------------------------------------------------------
max_score:99分,min_score:55分,average_score:77.0分
小结:处理文件数据一定要把控好每一个数据的细节!
标签:读取,min,max,average,score,line,平均分,21 From: https://www.cnblogs.com/LoLong/p/16863100.html