Question: Question 9 - Reading Malformed . csv Files Note: The only kinds of issues you need your function to handle are comma and quote misplacements;

Question 9- Reading Malformed .csv Files Note:
The only kinds of issues you need your function to handle are comma and quote misplacements; don't try and find any other issues with the CSV.
With that said, you should assume that data/malformed.csv is a sample of a larger file that has the same sorts of errors, but potentially in different lines. For example,
data/malformed.csv has an unnecessary quote " in line 4, but your function may be called on another CSV that has a perfectly fine line 4 but an unnecessary quote
on some other line.
So, don't implement parse_malformed assuming that the commas and quotes are mispositioned on specific lines; rather, implement parse_malformed such that it
can handle these issues on every single line they appear in.
A good way to proceed is to open data/malformed.csv and look carefully at the comma and quote placements.
The first few rows of parse_malformed('data/malformed.csv') should be: fp = Path('data')/ 'malformed.csv'
cols =['first', 'last', 'weight', 'height', 'geo']
df = parse_malformed(fp)
dg = pd.read_csv(fp, nrows=4, skiprows=10, names=cols)
data/malformed.csv is a file of comma-separated values, containing the following fields:
Unfortunately, the entries contains errors with the placement of commas (,) and quotes that cause pandas ' read_csv function to fail parsing the file with the
default settings. Don't believe us? Try using pd.read_csv on data/malformed.csv and look at what happens.
As a result, instead of using pd.read_csv, you must read in the file manually using Python's built-in open function.
Complete the implementation of the function parse_malformed, which takes in a file path (fp) and returns a parsed, properly-typed DataFrame with the information in
the corresponding file. For example, fp may be 'data/malformed.csv'. The DataFrame should contain the columns described in the data description table above (with
the specified types).
Question 9 - Reading Malformed . csv Files Note:

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!