Printing a list from a file

A forum for general discussion of the Python programming language.

Printing a list from a file

Postby zacksolutions » Fri Aug 16, 2013 1:39 am

I was able to successfully store a list of player's name and scores in a file.
I would like to sort the list returned from a file in descending score order and show only the top 5 highest scores.

Here is the code I have:

Code: Select all
def high_scores(name, score):
    """Records a player's score"""
   
    high_scores = open("high_scores.txt", "a")
    scorelist = (name, ", ", str(score), "\n")
   
    high_scores.writelines(scorelist)
    high_scores.close()

    high_scores = open("high_scores.txt", "r")
    plist = high_scores.readlines()
    #sort the list highest to lowest ??
    plist(plist[:5]
    high_scores.close()

name = input("print your name: ")
score = int(input("enter your score: "))

high_scores(name, score)


Here is how it readlines() reads the contents from the file:

Code: Select all
['Peter, 10\n', 'Jane, 20\n', 'Freddy, 50\n', 'Lau, 60\n', 'Pat, 5\n']


Can someone kindly assist here?

TIA,
zacksolutions
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Printing a list from a file

Postby Mekire » Fri Aug 16, 2013 2:15 am

Code: Select all
raw = 'Peter, 10\nJane, 20\nFreddy, 50\nLau, 60\nPat, 5'
split_it = [pair.split(", ") for pair in raw.split("\n")]
sort_it = sorted(split_it, key=lambda x: int(x[1]), reverse=True)

print(sort_it)
Code: Select all
[['Lau', '60'], ['Freddy', '50'], ['Jane', '20'], ['Peter', '10'], ['Pat', '5']]

In your case raw would be the result of:
Code: Select all
raw = high_scores.read().strip()


You might however just want to consider using something like pickle/json/yaml to store your data rather than saving as plain text.
-Mek
User avatar
Mekire
 
Posts: 1018
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Printing a list from a file

Postby zacksolutions » Fri Aug 16, 2013 2:47 am

Hi Mekire,

Your solution worked!
Unfortunately, the challenge I am on in the book I am reading requests to do this without pickled object.

Here is the final code:

Code: Select all
def high_scores(name, score):
    """Records a player's score"""
   
    high_scores = open("high_scores.txt", "a")
    scorelist = (name, ", ", str(score), "\n")
   
    high_scores.writelines(scorelist)
    high_scores.close()

    high_scores = open("high_scores.txt", "r")
    raw = high_scores.read().strip()
    split_it = [pair.split(", ") for pair in raw.split("\n")]
    sort_it = sorted(split_it, key=lambda x:int(x[1].strip()), reverse=True)
    print('\n'.join('{0}: {1}'.format(*item) for item in sort_it[:5]))
    high_scores.close()

name = input("print your name: ")
score = int(input("enter your score: "))

high_scores(name, score)


Thanks again,
zacksolutions
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests