0

I have a .txt file that has the following information in it that displays a users name and then 3 scores that they have scored in a quiz:

callum,10,7,9
carl,10,10,10
hollie,1,4,7
brad,4,8,3
kyle,7,2,0

I would like to sort it into alphabetical order displaying the users highest score after their name.

9
  • I have opened the txt file by using : file = open('scores.txt', 'r') Commented Jan 23, 2015 at 9:13
  • Give more information on input file. callum,10,7,9 and carl,10,10,10 are on same line?? Commented Jan 23, 2015 at 9:15
  • No each different user is on a separate line, it wont let me post a picture though because i dont have enough rep Commented Jan 23, 2015 at 9:16
  • @Neenan37 It's probably going to flag as unclear what you asking Commented Jan 23, 2015 at 9:18
  • @Neenan37 Because too many informations are missing. You have to show us some codes here. How is your input-output etc. Instead of trying to explain them in comments, write them in your question please. Commented Jan 23, 2015 at 9:20

2 Answers 2

1
  1. Read file content.
  2. Use readlines() method to read lines from file.
  3. Use split() to get Name and score.
  4. Add in dictionary: Name is Key and Value is total score.
  5. Get all keys from the result dictionary.
  6. User sort() method to sort list by alphabets.
  7. Print result by alphabets order.

Code

p = "/home/vivek/Desktop/test_input.txt"
result = {}
with open(p, "rb") as fp:
    for i in fp.readlines():
        tmp = i.split(",")
        try:
            result[(tmp[0])] = eval(tmp[1]) + eval(tmp[2]) + eval(tmp[3]) 
        except:
            pass

alphabetical_name =  result.keys()
alphabetical_name.sort()

for i in alphabetical_name:
    print "Name:%s, Highest score: %d"%(i, result[i])

Output:

$ python test.py 
Name:brad, Highest score: 15
Name:callum, Highest score: 26
Name:carl, Highest score: 30
Name:hollie, Highest score: 12
Name:kyle, Highest score: 9
Sign up to request clarification or add additional context in comments.

Comments

0

So i would first of all isolate all the lines:

with open('filename') as f:
    lines = f.readlines()

So i will continue assuming I have a list called lines with the following content:

lines = ["callum,10,7,9","carl,10,10,10","hollie,1,4,7","brad,4,8,3","kyle,7,2,0"]

Then I will firstly sort the line by name

lines = sorted(lines)

Then for each line you want to isolate the marks, sort them and print them back:

for line in lines:
    #name is what there is before the first comma
    name = line[:line.find(",")]
    #marks are what there is after the second comma and are comma separated
    marks = line[line.find(",")+1:].split(",")
    #sort the marks
    marks = sorted(marks,key=int)

    #if you want to print only the highest
    print "%s,%s"%(name,marks[-1])

6 Comments

it says invalid syntax name = line.[:line.lfind(",")] on the first square bracket
sorry... no . before the bracket... it was just a typo
fixed that, but its printing their first score in the text file, not their highest score
can you print marks just before sorting them and just after and paste the result?
its printing off: brad,4 callum,7 carl,10 hollie,4 kyle,2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.