Question: Write a function named read _ file _ to _ list ( ) that accepts three parameters path, schema, and sep as described above. The

Write a function named read_file_to_list () that accepts three parameters path, schema, and sep as described above.
The function should perform the following steps:
Use with, open(), and read() to read the contents of the file into a string named contents.
Use split() to separate the string into a list named lines by splitting on the newline character.
Create an empty list named data. This will eventually contain the list that is to be returned.
The first line contains header information and will be processed differently from the other lines. Split the first string in lines on sep. Store the resulting list into the list data.
We no longer need the first element of lines. For convenience, you may delete it.
Loop over the remaining elements of lines. Each time the loop executes, use the function process_lines() to process the current line, appending the resulting list into data. Use the values provided to the parameters schema and sep.
Return data.
We will now test the read_file_to_list() function on two small data files. We will start with a data file that contains 10 observations from the Diamonds Dataset. Make sure that the file diamonds_partial.txt is in the same directory as your notebook. I suggest opening this file so that you can see what its contents look like.
Use read_file_to_list() to read the contends of the file diamonds_partial.txt. Individuals values within the rows of this data file are separated by commas. The datatypes for the columns in this data set are given by the following schema: [float, str, str, str, int]. Store the resulting list in a variable named diamond_data. Use a loop to print the lists contained in diamond_data with each list appearing on its own line.
We will now test our function with a file containing 10 observations from the Titanic Dataset. Make sure that the file titanic_partial.txt is in the same directory as your notebook. I suggest taking a look at the contents of this file.
Use read_file_to_list() to read the contends of the file titanic_partial.txt. Individual values within the rows of this data file are separated by tab characters. The datatypes for the columns in this data set are given by the following schema: [int, int, str, str, int, float]. Store the resulting list in a variable named titanic_data. Use a loop to print the lists contained in titanic_data with each list appearing on its own line.
Write a function named read _ file _ to _ list (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!