My code at the moment looks like this:
import csv #this imports the CSV module, which enables us to read the file easier than using file.readlines()
score_dict = {} #this creates an empty dictionary
class_file = open('Class-A.txt','r') #this opens the file and reads the lines inside
scores = csv.reader(class_file) #this stores the class_file data as a readable object (that can be stripped even though it is a list) into the variable scores
for line in scores: #this loops through the different scores in each line
if line: #this ignores and empty rows of text in the file
scores_list = [] #this creates an empty array that will contain the list of scores for each student
for key, column in enumerate(line):
if key != 0: #this ignores the first column of text in the file as that will be used as the key
scores_list.append(int(column.strip())) #this appends the array to containing scores that have been stripped of whitespace and newlines. It also converts the scores into integers because in the text file, the scores are strings.
score_dict[line[0]] = scores_list #this inserts the list of scores into the dictionary
exit
for key in sorted(score_dict):
print ("%s: %s" % (key, score_dict[key]))
I have to print each student's highest score in alphabetical order according to their names.
How can I sort the values in each key
?