【发布时间】:2015-10-15 02:51:37
【问题描述】:
我正在尝试遍历这样组织的文本文件:
学生 1 姓名
一年级学生
学生 2 姓名
学生二年级
...
学生 N 姓名
学生N级
一旦我在一行中找到了一个学生的名字,我该如何更改他的成绩?这是我想出的代码,但我不知道如何更改学生姓名后面的行。
gradebook = open('gradebook.txt', 'r')
studentName = input("What is the students name?")
for line in gradebook:
if line.rstrip() == studentName:
#I want to insert code here that would change the text on the line
#after the line where studentName is found.
else:
print("The student was not found.")
【问题讨论】:
标签: python for-loop file-io iteration