python - Go to next iteration after finding string by looping through text file? -
i'm trying iterate through text file organized this:
student 1 name
student 1 grade
student 2 name
student 2 grade
...
student n name
student n grade
once i've found students name on line, how can change grade? here's code i've come with, can't figure out how change line following students name.
gradebook = open('gradebook.txt', 'r') studentname = input("what students name?") line in gradebook: if line.rstrip() == studentname: #i want insert code here change text on line #after line studentname found. else: print("the student not found.")
temp = [] open('data', 'r') f: line in f: if "student 2" in line: try: # student 2 grade line = next(f) temp.append(changed_line) # in case student name last line of file except stopiteration: break else: temp.append(line) # save changes file open('data', 'w') f: line in temp: f.write(line)
Comments
Post a Comment