用python代码编写以下程序 利用faker编写出文件“成绩表.csv”,采用python文件相关函数和方法完成如下要求: 1.读取文件数据,计算每个学生的最终成绩 最终成绩=平时成绩乘以0.2+期中成绩乘以0.3+期末成绩乘以0.5 2.将学生姓名和计算得到最终成绩写入另一个文件“score.csv”,形如: 姓名,最终成绩 魏勇,58林建军,68
时间: 2023-05-30 22:06:13 浏览: 348
```python
from faker import Faker
import csv
fake = Faker(locale='zh_CN')
# 生成成绩数据
data = []
for i in range(20):
name = fake.name()
usual_score = fake.random_int(min=50, max=100)
midterm_score = fake.random_int(min=50, max=100)
final_score = fake.random_int(min=50, max=100)
score = round(usual_score*0.2 + midterm_score*0.3 + final_score*0.5, 2)
data.append([name, usual_score, midterm_score, final_score, score])
# 写入文件“成绩表.csv”
with open('成绩表.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['姓名', '平时成绩', '期中成绩', '期末成绩', '最终成绩'])
writer.writerows(data)
# 读取文件数据,计算每个学生的最终成绩,写入文件“score.csv”
with open('成绩表.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过表头
scores = []
for row in reader:
name = row[0]
usual_score = float(row[1])
midterm_score = float(row[2])
final_score = float(row[3])
score = round(usual_score*0.2 + midterm_score*0.3 + final_score*0.5, 2)
scores.append([name, score])
with open('score.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['姓名', '最终成绩'])
writer.writerows(scores)
```
阅读全文