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 readfiletolist 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 processlines 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 readfiletolist function on two small data files. We will start with a data file that contains observations from the Diamonds Dataset. Make sure that the file diamondspartial.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 readfiletolist to read the contends of the file diamondspartial.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 diamonddata. Use a loop to print the lists contained in diamonddata with each list appearing on its own line.
We will now test our function with a file containing observations from the Titanic Dataset. Make sure that the file titanicpartial.txt is in the same directory as your notebook. I suggest taking a look at the contents of this file.
Use readfiletolist to read the contends of the file titanicpartial.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 titanicdata. Use a loop to print the lists contained in titanicdata with each list appearing on its own line.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
