统计上一题文件score.txt文件中所有两门以上(包括两门)课程不及格的学生信息输出到文件failed.txt,其他的输出到文件pass.txt
时间: 2024-03-05 21:52:26 浏览: 56
现有student.txt和student-score.txt 将两个文件上传到hdfs上 使用Map/Reduce框架完成下面
好的,我会根据您的要求进行操作。以下是实现代码:
```python
# 打开 score.txt 文件
with open("score.txt", "r") as f:
lines = f.readlines()
failed_students = [] # 存放不及格学生信息的列表
pass_students = [] # 存放及格学生信息的列表
# 遍历每一行学生成绩信息
for line in lines:
info = line.strip().split()
name = info[0]
scores = info[1:]
# 统计不及格科目数量
count = 0
for score in scores:
if int(score) < 60:
count += 1
# 判断是否两门及以上不及格
if count >= 2:
failed_students.append(name + " " + " ".join(scores))
else:
pass_students.append(name + " " + " ".join(scores))
# 写入 failed.txt 文件
with open("failed.txt", "w") as f:
for student in failed_students:
f.write(student + "\n")
# 写入 pass.txt 文件
with open("pass.txt", "w") as f:
for student in pass_students:
f.write(student + "\n")
```
请注意,程序将会读取当前目录下的 score.txt 文件,并将符合条件的学生信息输出到 failed.txt 和 pass.txt 文件中。如需修改文件路径,请自行更改代码中文件名的部分。
阅读全文