I have a text file:
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
output = []
with open('file_name') as f:
for i in f.readlines():
for j in words_to_split:
i = ''.join(i.split(j))
output.append(i)
line.split() gives you a list which will be added to your lst list as a list object. Therefore rather than using lst.append(line) use lst.extend(line) for the correct output.
I understand what you are trying to achieve. Instead of the way you wrote it, here is a simpler approach:
import re
ls=set(re.findall(r"[\w']+", text)) #text is the input
print(sorted(ls))
Tested it to make sure it works:
EDIT:
I modified your code a bit to satisfy your use case.
fh = open(raw_input("Enter file name: "),'r')
lst = list()
for line in fh:
words = line[:-1].split(" ")
for word in words:
if word not in lst:
lst.append(word)
print(sorted(lst))
Output:
Enter file name: file.txt
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grie', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
Hope that solves your problem.