In: Computer Science
def get_words(filename):
'''
(str) -> list of str
Given the name of a file which contains many words
(one word per line), return a list of all these words.
The file may have comments and a blank space at the beginning
of the file, which should be ignored. All comment lines start
with
a semicolon ';'.
'''
Python
TEXTFILE Content :
;efsf
;gwe
privacy
pepper
abolish
desk
;hk
introduce
finish;
quarter
courage
fabricate
duck
amputate

def get_words(filename):
words = [] #To store the words
#open file in read mode
with open(filename, "r") as inputFile:
#store all lines in fileContent
fileContent = inputFile.read().splitlines()
#loop through each line
for line in fileContent:
#Check for comments (;) and blank lines( )
if(line== "" or line[0] == ' ' or line[0]==';'):
continue
else:
#Add the line to the list
words.append(line)
#return list of string
return words
######################### TEST ###########################
print()
words = get_words("data.txt")
for w in words:
print(w)