0

I have the following code:

test=open("NewTextDocument.txt", "r")
lines1 = (test.readlines())
lines1.sort()
print(lines1)`

And I am using it to store a text file with these contents:

('lars', ' in class', '1', ' has got a score of', 8)
('lars2', ' in class', '1', ' has got a score of', 1)
('as', ' in class', '1', ' has got a score of', 1)
('12', ' in class', '1', ' has got a score of', 0)
('lars', ' in class', '1', ' has got a score of', 8)
('lars', ' in class', '1', ' has got a score of', 8)
('lars', ' in class', '1', ' has got a score of', 7, ' with a time of', 39.79597997665405)
('test', ' in class', '1', ' has got a score of', 1, ' with a time of', 17)

What I want to do is sort the lines of the file alphabetically yet keep the line breaks. For example:

('as', ' in class', '1', ' has got a score of', 1)
('lars', ' in class', '1', ' has got a score of', 8)
('lars2', ' in class', '1', ' has got a score of', 1)

However what I get after running my code is:

["('12', ' in class', '1', ' has got a score of', 0)\n", 
"('as', ' in class', '1', ' has got a score of', 1)\n", 
"('lars', ' in class', '1', ' has got a score of', 7, ' with a time of', 39.79597997665405)\n", 
"('lars', ' in class', '1', ' has got a score of', 8)\n", 
"('lars', ' in class', '1', ' has got a score of', 8)\n", 
"('lars', ' in class', '1', ' has got a score of', 8)\n", 
"('lars2', ' in class', '1', ' has got a score of', 1)\n", 
"('test', ' in class', '1', ' has got a score of', 1, ' with a time of', 17)"]

How can I fix this?

kassak
  • 3,974
  • 1
  • 25
  • 36
Lars
  • 3
  • 1
  • 2

2 Answers2

1

All you're doing wrong is printing a list without any formatting. Printing lines1 will just spit out a long line with all the contents of that list. You want to loop over the list and print one line at a time:

for line in lines1:
    print line,

I added the comma since all of your lines end with a newline character anyway, so printing with a trailing comma means that it wont add an extra newline every time.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
  • Yeah it works now, thanks so much :) out of curiosity what would i need to do if i wanted to sort it by the scores, with the lowest first and highest last or vice versa? – Lars May 12 '15 at 09:46
  • You'd need to use split() to turn each line into a list and then sort by a specific index. [This question](http://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples?rq=1) has a lot of info about sorting a list of lists. – SuperBiasedMan May 12 '15 at 09:51
0

just add,

for line in lines1:
    print line 

complete code will become

>>> with open("results.txt") as test:
...     lines = test.readlines()
...     lines.sort()
...     for i in lines:
...             print i

output

('12', ' in class', '1', ' has got a score of', 0)

('as', ' in class', '1', ' has got a score of', 1)

('lars', ' in class', '1', ' has got a score of', 7, ' with a time of', 39.79597997665405)

('lars', ' in class', '1', ' has got a score of', 8)

('lars', ' in class', '1', ' has got a score of', 8)

('lars', ' in class', '1', ' has got a score of', 8)

('lars2', ' in class', '1', ' has got a score of', 1)

('test', ' in class', '1', ' has got a score of', 1, ' with a time of', 17)
marmeladze
  • 6,468
  • 3
  • 24
  • 45