Python 3.5.1 I am creating a program that sorts a text file. The program should re-write the text file to keep only the three latest scores for each student. I currently have 4 scores for 'Aum Patel' in the text file; I want python to remove the oldest version of the score before it has been read. The text file is as follows-> 'name','score'
This is the text file (it is called 'Quiz-1'):
Aum Patel,10
Guy,9
Aum Patel,8
Bob Singh,2
Aum Patel,4
Aum Patel,10
Chong Singh,1
Bob Singh,7
And this is the Python 3 code:
import csv
import operator
n=0
quizGrades = open('Quiz-1.txt' , 'r')
grades = csv.reader(quizGrades, delimiter =',')
sortedGrades = sorted(grades, reverse= False, key=operator.itemgetter(0))
person=[]
SCORE=[]
for eachline in sortedGrades:
person.append(eachline[0])
SCORE.append(eachline[1])
print(person[n],': scored ',SCORE[n])
print()
n=n+1
quizGrades.close()