Now the work is starting to get real. Write a function called read_response_file
ID: 3847259 • Letter: N
Question
Now the work is starting to get real. Write a function called read_response_file that takes one argument, a string
representing the name of a response file. Remember that the response file is tabular, like this:
00000000,key,d,d,b,e,e,d,d,a,d,d,d,b,c,a,b,e,c,c,b,c,c,b,e,c,c,e,d,d,d,d
10021795,Samden Cross,d,d,b,e,e,e,d,a,d,b,d,b,c,a,a,e,c,c,b,c,d,b,e,c,c,e,d,d,d,c
11051158,Jenni Nuuxon,d,d,b,e,e,d,d,a,d,b,b,b,c,a,b,b,c,c,b,c,c,b,a,c,c,e,d,d,d,a
Don’t do anything fancy with the data. Just strip the newline off the end of each line (use the rstrip() method), and split
each line into a list. Keep in mind that here, the data is separated by commas, not spaces. Each line in the file becomes a list, and the whole file should be represented a list
of lists. Also, the first line of the file represents the answer key; keep the key’s line in your list of lists. We’ll take it out
later.
Before going on, you want to be sure that your function works properly. Add the following testing code to your program (you
can find this code in a3q2-testing.py)
Explanation / Answer
Please find the funtion defntion below . The need of each line is commented along. rstrip() is used to remove the new line . and split() convers the line into list. append() is used to make list of list def read_response_file(file_name): # define the funtion mylist = [] with open("test") as data: # open the file name and store in variable data for line in data: line =line.rstrip()# remove end of line line =line.split(',')#proces each line to make it to a list mylist.append(line)#appent the line list to create list of list print(mylist) #print the list read_response_file("test")#test the code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.