新建一个文本文件score.csv,用来保存10名考生3门课程成绩。用Python读取文件内容,统计每门课程的平均分、最高分和最低分。(数学,语文,英语)
时间: 2024-01-22 21:20:47 浏览: 96
以下是代码实现:
```python
import csv
with open('score.csv', 'r') as f:
reader = csv.reader(f)
headers = next(reader) # 获取表头
scores = [[float(x) for x in row] for row in reader] # 将读取的每一行转换为浮点数列表
# 计算每门课程的平均分、最高分和最低分
math_scores = [row[0] for row in scores]
chinese_scores = [row[1] for row in scores]
english_scores = [row[2] for row in scores]
math_avg = sum(math_scores) / len(math_scores)
chinese_avg = sum(chinese_scores) / len(chinese_scores)
english_avg = sum(english_scores) / len(english_scores)
math_max = max(math_scores)
chinese_max = max(chinese_scores)
english_max = max(english_scores)
math_min = min(math_scores)
chinese_min = min(chinese_scores)
english_min = min(english_scores)
# 输出结果
print('数学:平均分={:.2f},最高分={},最低分={}'.format(math_avg, math_max, math_min))
print('语文:平均分={:.2f},最高分={},最低分={}'.format(chinese_avg, chinese_max, chinese_min))
print('英语:平均分={:.2f},最高分={},最低分={}'.format(english_avg, english_max, english_min))
```
注意,需要将每一行读取出来的字符串转换为浮点数,才能进行数值计算。
阅读全文