In: Computer Science
def read_words(filename, ignore='#'):
""" Read a list of words ignoring any lines that start with the
ignore character as well as any blank lines. """
return ['a', 'z']
How would I code this in Python?
#function for readwords
#input: filename adn ignore char
#returns a list of all lines
def read_words(filename,ignore='#'):
#open file in read mode
f = open(filename,'r')
#read all the lines from file
allLines = f.readlines()
#initialize an array to store our result
ListofLines = []
#iterate through each line
for line in allLines:
#if it is not an empty line and if it does'nt have # append to our list
if(line != '\n' and line.find('#') != 0):
ListofLines.append(line.rstrip('\n'))
return ListofLines
print(read_words('test.txt',ignore='#'))
----------------------------------------------------------------------------------------------------
Your ThumbsUp on this answer matters to me a lot :)
----------------------------------------------------------------------------------------------------
For any further clarifications, Please do not hesitate to reach out
in the comments section