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?