Как записать вывод в файл, который позже можно будет прочитать как список, отсортированный по второму элементу.
Входные данные в файле Scores.txt:
test 1: 1
test 2: 5
test 3: 2
test 4: 6
программа:
def sorter():
scores = "scores.txt"
highScores = list() # place all your processed lines in here
with open(scores) as fin:
for line in fin:
lineParts = line.split(": ")
if len(lineParts) > 1:
lineParts[-1] = lineParts[-1].replace("\n", "")
highScores.append(lineParts) # sorting uses lists
highScores.sort(key = lambda x: x[1])
print(highScores)
выходы:
[['test 1', '1'], ['Test 3', '2'], ['Test 2', '5'], ['Test 4', '6']]