Question

In: Computer Science

def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...

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?

Solutions

Expert Solution

#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


Related Solutions

In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
Make a program to read a file, filled with words separated by new lines, from command...
Make a program to read a file, filled with words separated by new lines, from command line arguments (args[0]), print the file, and then add each word into an arraylist. After making the arraylist, iterate through it and print its contents.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT