Skip to content
Advertisement

Order CSV with two digit numbers

I’m trying to create a leaderboard for a game, where after the game has been played, the python script accesses a CSV file (not ordered) and prints out the top 5 people with the highest score. Python seems to do this fine with single-digit numbers, but I can’t get it to work with 2 digit numbers. Here is the code:

JavaScript

and here is the output: (I just used placeholder names for now)

JavaScript

and here is the original leaderboard file:

JavaScript

How do I get it to order the numbers properly?

Advertisement

Answer

I suggest simply reading the file with file.readlines() and then processing the finished results.

Input File:

JavaScript

Code:

JavaScript

To print out only the first five scores from the ordered list sorted_data, use a for x in range() loop:

JavaScript

This for loop will run 5 times. It unpacks the name and score contained in the tuple stored at sorted_data x, and then prints out that data.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement