Question: #Please Answer in python codeblock A) Define a method called process_file that reads the .csv file as a list of lists, then converts that list
#Please Answer in python codeblock
A) Define a method called process_file that reads the .csv file as a list of lists, then converts that list of lists into a dictionary. The keys in the dictionary will be country names and the values are instances of Nation that represent that country. process_file should then return that dictionary.
If you are using Google Colab, there are a few ways to get your local files into your colab, but the simplest is to upload the .csv file to your colab. On the left-hand side of the colab UI you should see a folder symbol. Clicking on that symbol will open up the Files tab with a default folder called sample_data (which you can ignore). Simply drag and drop the .csv for this assignment to the root Files directory alongside sample_data, or use the Upload button to select the file. If the .csv file is called nations.csv, the path in Google Colab is therefore /content/nations.csv.
Regardless of your Notebook editor, the path variable in the provided code below should be the path to wherever your .csv file is. Change this to match your directory structure as necessary. You may use this code directly in your code cell to read the file in as a list of lists.
path = "/content/nations.csv" with open(path) as file: data = [line.rstrip(" ").split(",") for line in file.readlines()] The each list has the information for a particular country in the order of country, continent, population, land area. Here are the first few entries of the list of lists:
[['Afghanistan', 'Asia', '31.8', '251772'], ['Albania', 'Europe', '3.0', '11100'], ['Algeria', 'Africa', '38.3', '919595'], ...
B) Test your process_file method by printing out the first 10 Nation instances stored in the dictionary. You should see your string representation for each nation.
C) Define a function list_countries that takes a continent as a parameter and produces a list of countries that reside in that continent. Naturally, you should called your process_file method to first get the dictionary of info on each nation, then iterate over that dictionary to produce a list of countries specific to the given continent.
Test your method by calling, say, list_countries("South America") (feel free to test out other continents). Your output should look something like:
['Argentina', 'Bolivia', 'Brazil', 'Chile', 'Colombia', 'Ecuador', 'Guyana', 'Paraguay', 'Peru', 'Suriname', 'Uruguay', 'Venezuela']
D)
Again using your process_file method, iterate over the dictionary of nations to print the data in a tabular format. To keep the output short, print the information on, say, the first 15 nations stored in the dictionary. Format your output so the data is easily readable.
Your output should look something like:
Country Name Continent Population Land Area Afghanistan Asia 31.8 251772 Albania Europe 3.0 11100 Algeria Africa 38.3 919595 Andorra Europe .085 181 Angola Africa 19.1 481354 Antigua and Barbuda North America .091 108 Argentina South America 44.0 1068302 Armenia Asia 3.1 11506 Australia Australia/Oceania 22.5 2967909 Austria Europe 8.2 32383 Azerbaijan Asia 9.7 33436 Bahamas North America .32 5358 Bahrain Asia 1.3 253 Bangladesh Asia 166.3 55599 Barbados North America .29 167
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
