Question

In: Computer Science

Language: Python 3 Compose a function process which accepts a string filename. process should return a...

  • Language: Python 3
  • Compose a function process which accepts a string filename. process should return a list of records contained in the file.
  • #define your function here
  • # Create a blank list called `entries` and an empty string called `current_style`.
  • # Open the file `filename`, read the data using readlines(), and close it.
  • # Loop through each line of the file and do the following:
  • # Strip the whitespace off of the ends of the line using the `strip` method.
  • # If the line is blank, `continue` execution.
  • # If a line starts with `#`, it contains the musical style to be assigned to the musicians below
    # until the next line with `#`.
    # In this case, remove the `'#'` from the beginning of the string and assign the musical style to `current_style`.
    # The loop should then `continue`.
  • # Otherwise, a line contains a blues musician. In this case, process the record much as you did above,
    # except that you also need to add a musical style to the tuple as the third element.
    # These data should be appended to the list `entries` as a single entry in the form of a tuple,
    # (surname, first_name, current_style)

      
    # Finally, `return` the list `entries`.

  • The input is from a txt file

  • # it should pass this test
    # test basic single-genre case
    zydeco = process('./data/zydeco.txt')
    assert zydeco[0] == ('Chavis', 'Boozoo', 'Zydeco')
    assert zydeco[-3] == ('Dopsy', "Rockin'", 'Zydeco')

  • file zydeco.txt:

  • #Zydeco
    Boozoo Chavis
    C. C. Adcock
    Chris Ardoin
    Scott Billington
    Dudley Broussard
    Chubby Carrier
    Roy Carrier
    C. J. Chenier
    Chubby Carrier
    Geno Delafose
    Canray Fontenot
    Keith Frank
    Queen Ida
    Beau Jocque
    Leftover Salmon
    Rockin' Sidney
    Terrance Simien
    Andre Thierry
    Cedric Watson
    Buckwheat Zydeco
    Nathan Williams
    Guyland Leday
    Lil' Nate
    Leon Chavis
    Mo' Mojo
    Kenne' Wayne
    Rockin' Dopsy
    Amede Ardoin
    Clifton Chenier

Solutions

Expert Solution

RAW CODE

def process(filename):
                a = open(filename,"r")
                b = a.readlines() ### read line by line until \n character
                words = [x.strip() for x in b] ### remove \n character
                words = [x for x in words if x] ### In case list have null value, then remove
                data = []
                for word in words:
                        if word[0] == '#':
                                music_type = word[1:] ## If starts with # then keep it as music type
                        else:
                                *first, last =  word.split(" ") ### * indicates more than one value
                                tup = (last, " ".join(first), music_type) ## making them tuples
                                data.append(tup) ### appending tuple in list


                return data


zydeco = process('zydeco.txt')
assert zydeco[0] == ('Chavis', 'Boozoo', 'Zydeco')
assert zydeco[-3] == ('Dopsy', "Rockin'", 'Zydeco')
print(zydeco)

SCREENSHOTS (CODE WITH OUTPUT)

##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####


Related Solutions

3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string....
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string. The function should return a greeting message based on the hour given in the time object as follow: If the time is before noon: 'Good Morning.', if it is in the afternoon: 'Good Day.' Please use the following template for your python script to define the greetings() function and name it as mt_q3.py. Replace the place holder [seneca_id] with your Seneca email user name....
Write a program in python language, which accepts 2 numbers and a + sign (for addition)...
Write a program in python language, which accepts 2 numbers and a + sign (for addition) A sign - (for subtraction) A sign * (for multiplication), / (for division) Then calculate and to display the result of the operation he chose with the two numbers. Displaying the appropriate message
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
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 Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its...
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its contents, closes the file,* and returns a histogram based on the letter frequencies in the given file. If no such file exists, your function should return an empty histogram (i.e., an empty dictionary {}). So for example, if the file nash.txt was in the same directory as char_hist3.py and had the following contents: I have never seen a purple cow, And I never hope...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT