So basically I have a txt file that look like this:
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief
And I need to sort the txt file so that it looks like the desired output:
Here's my code:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line.lstrip("\n")
line.rstrip()
words = line.split()
for ln in words:
lst.append(ln)
print(sorted(lst))
Why the words repeated so many times? I know it has something to do with spaces, but I really can't figure it out. Plz help :(