Question: Problem 1 : parse _ data ( filename ) : For this problem, write a function called parse _ data ( filename ) in the

Problem 1: parse_data(filename):
For this problem, write a function called parse_data(filename) in the fishing.py file that reads the data in filename, uses csv.DictReader to parse the file, and returns a dictionary containing the data for each country in the file.
Given the example CSV shown earlier, the dictionary returned from parse_data should look like the following:
{ "min_year": 1960, "max_year": 1961, "farmed": { "AMP": {1960: 321,1961: 333},}, "wild caught": { "AMP": {1960: 7777,1961: 8888},}, "consumption": { "AMP": {1960: None, 1961: 10.42},}, "population": { "AMP": {1960: 995623,1961: 996235},},}
This is a triply nested dictionary; that is, a dictionary with values as dictionaries where those values are also dictionaries. From outside in, the structure of the dictionary is as follows:
For the outermost dictionary:
The keys are the four different types of measurements: farmed, wild caught, consumption, and population.
The values are dictionaries representing the data for that specific measure.
For the second-level dictionary, for example the value of the 'farmed' key from the outmost dictionary:
The keys are the three-letter country codes represented in the data. For example, AMP,USA,MEX, etc. These match the "country code" column from the CSV files.
The values are dictionaries, representing the data specific to the key (i.e., country) for the measure its nested within.
For the innermost dictionary:
The keys are the years represented in the data. (E.g.,1960,1961) These should be integers.
The values are the measurements for that year or None if there is no data for that year (represented as empty string ('') from csv.DictReader). The types for these values should be as follows:
farmed, wild caught, and consumption should all be floatspopulation should be an integer
Consider as an example a subset of the above example dictionary: { "farmed": { "AMP": {1960: 321,1961: 333}}}. Reading this from the inside out we would say that in 1961, the country AMP had farmed 333 metric tonnes of fish.
You should use the min_year and max_year functions provided to you in utils.py (imported for you in the starter file) to get the range of years for which data is available. If youve created a csv.DictReader as reader = csv.DictReader(file) then you can set the min and max years like data["min_year"]= min_year(reader.fieldnames).
Tip
We recommend that you open up the CSV files and examine the data before writing code. It is normal and expected to receive data that is incomplete. Using csv.DictReader, those missing values will be given to you as empty strings; you should account for these in your code.
Tip
Although its not required, you may find it helpful to write some tests for this function to ensure it parses the file correctly and creates the expected dictionary structure correctly. Using the sample data for AMP, aka Ankh-Morpork, is a great test to write.

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!