-5

I'm trying to print data from my text file into python

                text_file = open ("Class1.txt", "r")
                data = text_file.read().splitlines()
                for li in data:
                    namelist = li.split(":")[0]
                    scorelist = li.split(":")[1]

                print (namelist)
                print (scorelist)
                text_file.close()

My text file has:

Jim:13524
Harry:3
Jarrod:10
Jacob:0
Harold:5
Charlie:3
Jj:0

It only shows the last entry

Shell:

Would you like to view class 1, 2 or 3? 1
Jj
0
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JJA
  • 5
  • 5
  • Would be helpful to see what's in the txt file. – user2097159 Dec 03 '14 at 13:17
  • I've added whats in my text file – JJA Dec 03 '14 at 13:19
  • For starters look into python dictionaries and how to sort them this will help you greatly – user2097159 Dec 03 '14 at 13:21
  • So what does your code so far do, and what should it be doing instead? – jonrsharpe Dec 03 '14 at 13:21
  • Do you really think you should be asking on Stack Overflow for us to do your homework? – Ffisegydd Dec 03 '14 at 13:22
  • 1
    @user2097159, What do you mean by that? Python dictionaries are unordered, so they can't be sorted. – Kevin Dec 03 '14 at 13:22
  • @Kevin Dictionaries for the pairing then somehthing like the following http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value – user2097159 Dec 03 '14 at 13:24
  • 1
    @user2097159 to quote the first line of the accepted answer: *"It is not possible to sort a dict, only to get a representation of a dict that is sorted."* – jonrsharpe Dec 03 '14 at 13:25
  • @jonrsharpe I had to make a 10 question maths quiz which is randomly generated (task1) I then had to save the score of the user to a text file (task2) For task 3, which I'm stuck on, I need to sort the results in the text file to highest - lowest, average in python – JJA Dec 03 '14 at 13:25
  • @Ffisegydd I'm not asking you to do it for me. I want help. I'm been stuck on it for quite a while and can't seem to find any solutions. – JJA Dec 03 '14 at 13:26
  • 2
    Part of your coursework will be to work out your own problems. Us writing code for you, and us giving you the ideas/examples for you to write your own code, are both cheating. – Ffisegydd Dec 03 '14 at 13:27
  • 1
    @JJA this is neither a code-writing nor a tutorial site. Please see http://stackoverflow.com/help/mcve and produce the *shortest code that recreates the problem* (e.g. if the input works, strip it out) and *precisely what is wrong with it* (e.g. error traceback, unexpected outputs). Read e.g. http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jonrsharpe Dec 03 '14 at 13:34

1 Answers1

0

The problem is that you are over-writing the value of namelist and scorelist with each pass through the loop. You need to add each item to a list. Adding a sequential list of items to a list is usually done with list.append() or a list comprehension. Read the documentation, or do some tutorials?

To actually create list, you can do this:

namelist, scorelist = [],[]
for li in data:
    namelist.append(li.split(":")[0])
    scorelist.append(li.split(":")[1])

Alternately, this might be a better overall approach:

        with open("Class1.txt", "r") as text_file:
            names_scores = [(e[0],e[1]) for e in [li.split(":") for li in text_file]

        for name,score in name_scores:
            print(name,score)

This assumes you really just want to extract the names and scores and print them, not do anything else. How you handle and store the data depends a lot on what you are doing with it once you extract from the file.

selllikesybok
  • 1,250
  • 11
  • 17
  • Thanks, it now prints my data. I'm planning on sorting this data now i have it working, will this work the first section of code? When i try the second section i get; "Invalid syntax" and highlights the "for" in the line "names_scores = [e[0],e[1] for e in [li.split(":") for li in text_file]" – JJA Dec 03 '14 at 15:31
  • Fixed the error in the name_scores line, left out some braces. – selllikesybok Dec 03 '14 at 16:22