组织和排序文本文件中的数据

Organising and sorting data from a text file

我根据参加考试的人和他们收到的分数将一些信息存储在文本文件中。每次他们再次参加考试时,都会添加一个新分数。文本文件看起来像这样,其中存储了数据:

Mike 5 7 9
Terry 6 6 9
Paul 4 5 6

我希望能够从文本文件中检索信息并确定每个人的最高分,以便打印出他们的名字和一个数字。

如果我从文件中检索数据并使用此代码将其存储为列表:

with open("classa.txt") as f:
   content = f.readlines()
   print(content)

然后打印出来的数据是这样的: ['Mike 5 7 9\n', 'Terry 6 6 9\n', 'Paul 4 5 6']

我猜我真的需要在一个列表中创建多个嵌套列表。每个人一个,但我不确定如何完成此操作或如何解析数据以便我可以在列中使用它并在处理它后面的数值时忽略 "Name" 列。

如果文本文件中的数据是逗号分隔的,这样读起来会更好吗:

Mike,5,7,9
Terry,6,6,9
Paul,4,5,6

如有任何帮助,我们将不胜感激。我有点不知所措。

with open("names.txt") as f:
    # splitlines of the content
    content = f.read().splitlines()
    for line in content:
        # split at spaces
        splittedLine = line.split(" ")

        # get the first element which is the name
        name = splittedLine[0]

        # get the all the elements except the first
        scores = splittedLine[1:]

        # get the last element of the sorted list which is the highscore
        highscore = sorted(scores)[-1]
        print("{} : {}".format(name, highscore))

我对代码进行了注释,希望大家能理解。

输出:

Mike : 9

Terry : 9

Paul : 6