用python代码编写以下程序 利用faker编写出文件“成绩表.csv”,采用python文件相关函数和方法完成如下要求: 1.读取文件数据,计算每个学生的最终成绩 最终成绩=平时成绩+期中成绩+期末成绩 2.将学生姓名和计算得到最终成绩写入另一个文件“score.csv”,形如: 姓名,最终成绩 魏勇,58林建军,68
时间: 2023-05-30 22:05:34 浏览: 124
```python
from faker import Faker
import random
import csv
fake = Faker('zh_CN')
# 生成成绩表.csv文件
with open('成绩表.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['姓名', '平时成绩', '期中成绩', '期末成绩'])
for i in range(20):
name = fake.name()
writer.writerow([name, random.randint(50, 100), random.randint(50, 100), random.randint(60, 100)])
# 读取成绩表.csv文件并计算最终成绩
with open('成绩表.csv', 'r', newline='', encoding='utf-8') as f1, open('score.csv', 'w', newline='', encoding='utf-8') as f2:
reader = csv.reader(f1)
writer = csv.writer(f2)
writer.writerow(['姓名', '最终成绩'])
next(reader) # 跳过表头
for row in reader:
name = row[0]
score = int(row[1]) * 0.3 + int(row[2]) * 0.3 + int(row[3]) * 0.4
writer.writerow([name, round(score)])
```
阅读全文