Python Forum
Problems Sorting Data in an External File (.txt)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems Sorting Data in an External File (.txt)
#1
I'm creating a Leaderboard for my Two-Player Dice Rolling Game, I want the Data inside the File to be Sorted in order and neatly presented.
Objectives:
  • Sorts the Scores High to Low
  • Saves the Player's Name and Score in External File
  • Prints out the Top 5 Winning Scores

My problems are:
  • It prints out the first line which I don't want it to as it outputs the Headings (you can copy the code and try it yourselves) and outputs the name's and scoring which is good but not in the right position
  • It overwrite the names and scores for each of the two players names and scores, every time I retry the program


I need the data neatly as shown below:
The dots are the Spaces

Position.....Name.......Score
.....1..........John.........25
.....2..........Adam.........56


# -------------- Leaderboard --------------

def leaderboard():

    file = open("Leaderboard.txt",'w')
    file.write("Position        " + "Name        " + "Score" + "\n") #<----- Heading
    file.close()

    file = open("Leaderboard.txt",'a')
    file.write((username_1) + ":" + (str(playerScore1)) + "\n") #<---- Stores Player 1's Name and Score
    file.write((username_2) + ":" + (str(playerScore2)) + "\n") #<---- Stores Player 2's Name and Score
    file.close()
    
    
    file = open("Leaderboard.txt",'r')
    readthefile = file.readlines()
    sortedData = sorted(readthefile, reverse = True)  #<---- Sorting the Data

    print("==============================")
    print("    Top 5 Winning Scores")
    print()

    for line in range (5):  #<---- Outputs the Top 5 Players
        print(str(line + 1) + "\t" + str(sortedData[line]))

leaderboard()

#------------- OUTPUT ------------
==============================
    Top 5 Winning Scores

1       John:25

2       Position        Name        Score

3       Adam:56
Reply
#2
I have Fixed the Issue now!!! Big Grin
The Heading Part wasn't required in the file.write
The Data was getting Sorted but not Correct now that is Fixed

username_1 = "Adam"
username_2 = "John"
playerScore1 = 56
playerScore2 = 45

def leaderboard():

    file = open("Leaderboard.txt", 'a')
    file.write((str(playerScore1)) + "       " + (username_1) + "\n")
    file.write((str(playerScore2)) + "       " + (username_2) + "\n")
    file.close()

    file = open("Leaderboard.txt", 'r')
    readthefile = file.readlines()
    sortedData = sorted(readthefile, reverse = True)

    print("==============================")
    print("    Top 5 Winning Scores")
    print()

    print("Pos        " + "Score     " + " Name")

    for line in range(5):
        print(" " + str(line + 1) + "\t    " + str(sortedData[line]))

leaderboard()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems writing a large text file in python Vilius 4 1,079 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,772 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
Sad problems with reading csv file. MassiJames 3 2,704 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Upgrading from 2 to 3 and having file write problems KenHorse 2 2,246 May-08-2022, 09:47 PM
Last Post: KenHorse
  Writing to External File DaveG 9 4,570 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Sorting table data Blacktime2 1 1,900 Feb-26-2022, 07:05 PM
Last Post: ibreeden
  File sorting by user-chosen category Bachelar 0 1,885 Aug-28-2021, 08:14 AM
Last Post: Bachelar
  How to send data from a python application to an external application aditya_rajiv 1 3,038 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  Problems with inserting images into an Excel File FightingFarmer 2 4,584 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  Sorting data pprod 4 3,647 Feb-01-2021, 06:51 PM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020