I must open a file and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort (Alphabetical order, with priority given to words with initial capitalization) and print the resulting words in python sort() order as shown in the desired output.For the moment my code is incomplete:
list=[]
fname = input("Enter file name: ")
try:
fh=open(fname)
except:
print('File cannot be opened:',fname)
quit()
fhand=open(fname)
for line in fhand:
value=line.rstrip()
list.append(value)
list.sort()
index=list[0].split()
print(index)
for the moment mu result is: ['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon']
- but I want obtain a result like this:
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
The text of file is:
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
I newbie in Python, but in this case I don't found, for the moment, a solution to obtain the result.I definitely need to study more about the various functions split, strip, append, etc.
CodePudding user response:
there are several problems:
- don't override
list
builtin, use for examplemy_list
- try/except have an
else
clause that only executes in case of success this allows to not opening twice your file - Finally, you only need to sort your values at the end, not at every loop iteration
Taking in consideration the above you can do something like this
my_list = []
fname = input("Enter file name: ")
try:
fh=open(fname)
except:
print('File cannot be opened:',fname)
else:
for line in fh:
split_line = line.split()
my_list.extend(split_line)
my_list.sort()
output
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']
CodePudding user response:
Previous answer does not close the file, and leaves duplicates as well. Try this:
fname = input("Enter file name: ")
try:
with open(fname) as f:
data = f.read() # read entire file at once as a huge string
except IOError as e:
print(f"Error opening a file {e}")
my_list = [d for d in data.split()]
my_list = list(set(my_list))
my_list.sort()
output:
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
Explanation: If you solely use the open() function, you have to close it later, using with statement to open the file will take care of closing for you. To get rid of duplicates the easiest way is to convert your list to a set, than back to a list.
CodePudding user response:
You can try something like this (use split and add to the list then sort the list):
fhand = open(fname)
l = []
for line in fhand.readlines():
l = line.split()
l.sort()