Skip to content Skip to sidebar Skip to footer

Organising And Sorting Data From A Text File

I've got some information stored in a text file based on people taking a test and the scores they have received. Every time they take the test again, a new score is added. The text

Solution 1:

withopen("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))

I commented the code so i hope everything is understandable.

Output:

Mike : 9

Terry : 9

Paul : 6

Post a Comment for "Organising And Sorting Data From A Text File"