Search code examples
pythonpython-3.xtextrankingleaderboard

Is there a method for switching several lines with each other in a text file?


I need a METHOD/technique to sort a text file document of players after how good their score is. So if player2 gets a better score than player 1, player 2 needs to move up to player 1's position in the text file and vice versa. Is this possible?

So I've found ways when the document lists different categories on each line so that it reads:

player1, 3, 2, 1
player2, 1, 2, 3

but my problem is that each player have 4 lines of code that needs to be switched. My text file looks like this:

player1
3 (*this is the one that matters)
2
1

player2
1 *
2
3

  • So lets say player 2 plays a lot and gets a higher score than player 1 the text document should look like

player2
4 (*)
3
2

player1
3 (*this is the line that matters)
2
1

  • So to clarify; I need to sort the document where the player with the highest score is written first in the text file. This is so that I can print the resulting leaderboard in question

  • Please note: I'm not asking for code, but a method/general example(or where to find one) that can be used to solve this type of problem. Bc I'm really at a loss, looked in my book and here. So if you have an answer to this or know of a answered thread with the same problem please comment it, it would help me a lot!

I know I dont have any code to supply, but that is bc there is none. I really just need some help in where to start / guide me towards a method or example

Thanks in advance :D


Solution

  • First, you can't easily modify this kind of file directly. The method is: read the data, modify the data, write the data back.

    I'll suppose that you can read your data into a data structure. For example, in your case the data could be naturally stored as a list of 4-tuples. Each 4-tuple would look like this:

        ("player1", 3, 2, 1)
    

    (You should make sure you have stored numbers for the scores, not strings, because they need to be compared later.)

    Let's suppose you've stored them in a list called player_list

    Next you need to sort your list. The sort key you need is the 2nd entry in each tuple, which in Python terms has index 1 (because numbering is from 0)

    def sort_key ( tup ): return tup[1]
    

    Then you can use this sort key to pass to the sort function (see this https://docs.python.org/3/library/stdtypes.html#list.sort ) This sorts your list in-place:

    player_list . sort ( key = sort_key, reverse = True )
    

    (We need "reverse" to make sure the order is decreasing, not increasing.)

    Finally you'll need to overwrite the file by writing out the player_list again, in the new order:

    for (player, s1, s2, s3) in player_list:
        write_entry_to_file ( player, s1, s2, s3 ) # use whatever format you need here.
    

    Note on Tuples:

    Tuples are a lot like lists, except that once you've made a tuple it can't be changed ("immutable"). To make a tuple you use parentheses () rather than square brackets [].

    mylist = [2, 4, 6, 8]
    mytuple = (2, 4, 6, 8)
    

    You can index them in the same way

    mylist[0] # returns 2
    mytuple[0] # returns 2 too
    mylist[1:3] # returns the list [4, 6]
    mytuple[1:3] # returns the tuple (4, 6)
    

    and append them in the same way

    [2,4] + [6,8] # returns [2,4,6,8]
    (2,4) + (6,8) # returns (2,4,6,8)
    

    The empty tuple is (), and the tuple with a single element x is (x,)

    converting between the two is easy:

    list(mytuple) # returns [2,4,6,8]
    tuple(mylist) # returns (2,4,6,8)