Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python Write a function that takes a filename and a target string as input parameters and returns a list of the lines in the

In python

Write a function that takes a filename and a target string as input parameters and returns a list of the lines in the file that contain the target string. The function uses the filename to open the file, loops through the lines of the file and saves each line that contains the target string as one element of the list. No printingshould be done inside the function - it should just return the list of lines that contain the target string. The function must have the following signature:

def getFileMatches(filename, target): 

It would be used as follows:

>>> filename = "/cs/faculty/mikec/cs8/lab07/songs.txt" >>> loveLines = getFileMatches(filename, "Love") >>> loveLines ['"When A Man Loves A Woman" - Percy Sledge ', '"Whole Lotta Love" - Led Zeppel in ', '"Sunshine Of Your Love" - Cream ', '"Bye Bye Love" - The Everly Brother s ', '"Somebody To Love" - Jefferson Airplane ', '"Where Did Our Love Go" - Su premes ', '"She Loves You" - Beatles ', '"(Love Is Like A) Heat Wave" - Martha & The Vandellas ', '"Why Do Fools Fall In Love?" - Frankie Lymon & Teenagers ', '"I\'d Love To Change The World" - Ten Years After ', '"Who Do You Love?" - Bo Diddley ', '"Stop! In The Name Of Love" - The Supremes ', '"What\'s Love Go t To Do With It?" - Tina Turner ', '"Love Hurts" - Nazarath ', '"Pride (In The name Of Love)" - U2 ', '"I Love Rock \'n\' Roll" - Joan Jett & The Blackhearts ', '"Love The One You\'re With" - Stephen Stills '] Then Write a function that takes in a filename and a target string as parameters, calls your getFileMatches function, stores the result into a list, and then prints out each element of the list. For example: 
>>> filename = "/cs/class/cs8/lab07/songs.txt" >>> printFileMatches(filename, "We") "We Will Rock You" - Queen "The Weight" - The Band "Welcome To The Jungle" - Guns N' Roses "Werewolves of London" - Warren Zevon "We're An American Band" - Grand Funk Railroad "We Gotta Get Out Of This Place" - Animals 

Don't worry about printing the extra newline characters that are part of each line. Of course, if that bothers you then you can get rid of the extra lines in more than one way. Let's say a line of the file is stored in a string called line, then any of the following would print just one of the two newline characters:

>>> print(line[:-1]) # prints all except the last character in line >>> print(line.strip()) # prints line with no leading/trailing white space >>> print(line, end='') # replaces the usual ' ' at the end with '' 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

Ability to work comfortably in a team environment

Answered: 1 week ago