0

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:

The output and 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 :(

Code.vc
  • 21
  • 4
  • `line.lstrip("\n")` does not change `line`, nor does `line.rstrip()`. – DYZ May 28 '20 at 01:53
  • 3
    What does this have to do with removing spaces? Your question is about how to remove duplicate elements from a list. – Barmar May 28 '20 at 01:53
  • There's no need to strip the line if you're using `line.split()`, since it will ignore the whitespace at the end. – Barmar May 28 '20 at 01:54
  • Use a `set` instead of a `list` if you don't want duplicate elements. – Selcuk May 28 '20 at 01:55

1 Answers1

0

You want to get a sorted list of unique words, so you should use a set() to filter out duplicates. It's also recommended to open / read the file using with open() so you don't forget to close your file stream.

fname = input("Enter file name: ")
with open(fname, 'r') as fh:
    words = set(fh.read().split())
print(sorted(words))
vanPelt2
  • 870
  • 6
  • 16