Question: Save your completed program happy3.py as happy4.py and comment out the call to the lookup_happiness_by_country() in the main() function. When you run the program it
Save your completed program happy3.py as happy4.py and comment out the call to the lookup_happiness_by_country() in the main() function. When you run the program it should have no output.
You will now start writing the function read_gdp_data() so that it reads the input file world_pop_gdp.tsv which contains the country name, population in millions, and the GDP per capita. The file contains a column header that should be ignored and looks like this:
Country Population in Millions GDP per Capita China 1,394.02 $18,192.04 United States 332.64 $58,592.03 India 1,326.09 $7,144.29 Japan 125.51 $43,367.94
You will write a loop that reads the file and creates new comma separated output printed to the screen. The commas must be removed from the numbers in comma separated output. Also, because some plotting programs cannot deal with the $, that will also be removed from the output. Do that using string method calls.
The first five lines of your programs printed comma separated output should look like this:
Country,Population in Millions,GDP per Capita China,1394.02,18192.04 United States,332.64,58592.03 India,1326.09,7144.29 Japan,125.51,43367.94 world_pop_gdp.tsv:
Country Population in MillionsGDP per Capita China 1,394.02 ######## United States 332.64 ######## India 1,326.09 $7,144.29 Japan 125.51 ######## Germany 80.16 ######## Russia 141.72 ######## Indonesia 267.03 ######## Brazil 211.72 ######## United Kingdom 65.76 ######## France 67.85 ######## Mexico 128.65 ######## Italy 62.40 ######## Turkey 82.02 ######## South Korea 51.84 ######## Spain 50.02 ######## Saudi Arabia 34.17 ######## Canada 37.69 ######## Iran 84.92 ######## Australia 25.47 ######## Thailand 68.98 ######## Egypt 104.12 ######## Taiwan 23.60 ######## Poland 38.28 ######## Nigeria 214.03 $5,237.63 Pakistan 233.50 $4,543.88 Malaysia 32.65 ######## Netherlands 17.28 ######## Argentina 45.48 ######## Philippines 109.18 $8,034.38 South Africa 56.46 ######## Colombia 49.08 ######## United Arab Emirates9.99 ######## Bangladesh 162.65 $4,244.06 Iraq 38.87 ######## Vietnam 98.72 $6,571.03 Algeria 42.97 ######## Belgium 11.72 ######## Singapore 6.21 ######## Switzerland 8.40 ######## Sweden 10.20 ######## Romania 21.30 ######## Hong Kong 7.25 ######## Kazakhstan 19.09 ######## Chile 18.19 ######## Austria 8.86 ######## Peru 31.91 ######## Venezuela 28.64 ######## Norway 5.47 ######## Czechia 10.70 ######## Ukraine 43.92 $8,414.74 Ireland 5.18 ######## Qatar 2.44 ######## Burma 56.59 $5,827.88 Israel 8.68 ######## Portugal 10.30 ######## Greece 10.61 ######## Morocco 35.56 $8,396.68 Kuwait 2.99 ######## Hungary 9.77 ######## Denmark 5.87 ######## Sri Lanka 22.89 ######## Finland 5.57 ######## Uzbekistan 30.57 $7,295.83 Ethiopia 108.11 $1,855.46 Angola 32.52 $5,952.83 Ecuador 16.90 ######## Oman 4.66 ######## New Zealand 4.93 ######## Slovakia 5.44 ######## Belarus 9.48 ######## Sudan 45.56 $3,893.63 Dominican Republic10.50 ######## Azerbaijan 10.21 ######## Kenya 53.53 $3,058.22 Tanzania 58.55 $2,775.27 Bulgaria 6.97 ######## Guatemala 17.15 $8,050.93 Tunisia 11.72 ######## Cuba 11.06 ######## Ghana 29.34 $4,567.11 Puerto Rico 3.19 ######## Serbia 7.01 ######## Panama 3.89 ######## Turkmenistan 5.53 ######## Croatia 4.23 ######## Cote d'Ivoire 27.48 $3,535.52 Lithuania 2.73 ######## Cameroon 27.74 $3,227.25 Uganda 43.25 $2,062.06 Jordan 10.82 $8,225.02 Paraguay 7.19 ######## Lebanon 5.47 ######## Costa Rica 5.10 ######## Bolivia 11.64 $7,192.50 Nepal 30.33 $2,611.13 Uruguay 3.39 ######## Macau 0.61 ######## Yemen 29.88 $2,463.83 Slovenia 2.10 ######## Bahrain 1.51 ######## Afghanistan 36.64 $1,895.27 Zambia 17.43 $3,955.44 Cambodia 16.93 $3,793.35 Luxembourg 0.63 ######## Libya 6.89 $8,993.50 Senegal 15.74 $3,482.38 Latvia 1.88 ######## El Salvador 6.48 $7,895.26 Syria 19.40 $2,591.96 Laos 7.45 $6,625.13 Honduras 9.24 $5,013.35 Bosnia and Herzegovina3.84 ######## Mongolia 3.17 ######## Trinidad and Tobago1.21 ######## Estonia 1.23 ######## Mali 19.55 $2,108.07 North Korea 25.64 $1,559.85 Georgia 4.00 $9,969.98 Madagascar 26.96 $1,478.35 Botswana 2.32 ######## Mozambique 30.10 $1,232.30 Gabon 2.23 ######## Nicaragua 6.20 $5,867.71 Albania 3.07 ######## Burkina Faso 20.84 $1,720.63 Zimbabwe 14.55 $2,355.92 Brunei 0.46 ######## Cyprus 1.27 ######## Equatorial Guinea0.84 ######## Macedonia 2.13 ######## Papua New Guinea7.26 $4,158.71 Chad 16.88 $1,695.76 Tajikistan 8.87 $3,203.86 Armenia 3.02 $9,379.99 Mauritius 1.38 ######## Guinea 12.53 $2,232.70 Namibia 2.63 ######## Jamaica 2.81 $9,278.74 Benin 12.86 $1,973.63 Rwanda 12.71 $1,941.41 Moldova 3.36 $7,050.09 Kyrgyzstan 5.96 $3,881.04 Malawi 21.20 $1,057.72 Niger 22.77 $959.94 West Bank 2.90 $7,317.16 Somalia 11.76 $1,738.52 South Sudan 10.56 $1,894.66 Haiti 11.07 $1,804.34 Kosovo 1.93 ######## Malta 0.46 ######## Iceland 0.35 ######## Mauritania 4.01 $4,314.10 Togo 8.61 $1,506.66 Eswatini 1.10 ######## Sierra Leone 6.62 $1,743.41 New Caledonia 0.29 ######## Montenegro 0.61 ######## Eritrea 6.08 $1,546.08 Suriname 0.61 ######## Fiji 0.94 $9,219.27 Burundi 11.87 $674.80 Monaco 0.04 ######## Timor-Leste 1.38 $5,366.68 Bhutan 0.78 $9,209.81 Maldives 0.39 ######## Isle of Man 0.09 ######## Lesotho 1.97 $3,379.82 Guyana 0.75 $8,399.05 Bermuda 0.07 ######## Liberia 5.07 $1,204.74 Guam 0.17 ######## Jersey 0.10 ######## French Polynesia 0.30 ######## Barbados 0.29 ######## Liechtenstein 0.04 ######## Aruba 0.12 ######## Virgin Islands 0.11 ######## Cabo Verde 0.58 $6,475.73 Djibouti 0.92 $3,948.78 Guernsey 0.07 ######## Central African Republic5.99 $565.86 Andorra 0.08 ######## Belize 0.40 $8,053.09 Guinea-Bissau 1.93 $1,645.47 Curacao 0.15 ######## Seychelles 0.10 ######## Saint Lucia 0.17 ######## Cayman Islands 0.06 ######## Greenland 0.06 ######## Antigua and Barbuda0.10 ######## San Marino 0.03 ######## Gibraltar 0.03 ######## Faroe Islands 0.05 ######## Grenada 0.11 ######## Saint Kitts and Nevis0.05 ######## Solomon Islands 0.69 $1,941.33 Comoros 0.85 $1,558.58 Saint Vincent and the Grenadines0.10 ######## Northern Mariana Islands0.05 ######## Samoa 0.20 $5,579.71 Western Sahara 0.65 $1,389.76 Dominica 0.07 ######## Vanuatu 0.30 $2,587.71 Sao Tome and Principe0.21 $3,249.31 American Samoa 0.05 ######## Turks and Caicos Islands0.06 ######## Tonga 0.11 $5,570.48 Saint Martin 0.03 ######## British Virgin Islands0.04 ######## Sint Maarten 0.04 $8,342.65 Cook Islands 0.01 ######## Palau 0.02 ######## Saint Pierre and Miquelon0.01 ######## Kiribati 0.11 $2,030.48 Falkland Islands (Islas Malvinas)0.00 ######## Marshall Islands 0.08 $2,515.50 Anguilla 0.02 $9,695.96 Montserrat 0.01 ######## Nauru 0.01 ######## Wallis and Futuna0.02 $3,784.53 Tuvalu 0.01 $3,703.05 Niue 0.00 $5,005.00 Tokelau 0.00 $910.75
happy3.py:
import csv
def main(): happy_dict = make_happy_dict() # print_sorted_dictionary(happy_dict) lookup_happiness_by_country(happy_dict) def make_happy_dict(): filename = "happiness.csv" happy_dict = {} with open(filename, 'r') as infile: csv_happy = csv.reader(infile) infile.readline() for line in csv_happy: happy_dict[line[0]] = line[2] return happy_dict
def lookup_happiness_by_country(happy_dict): while True: name = input("Enter a country to lookup or 'done' to exit:") if name == 'done': return if name in happy_dict: print(happy_dict[name]) else: print(name, 'not found')
def print_sorted_dictionary(D): if type(D) != type({}): print("Dictionary not found") return print("Contents of dictionary sorted by key.") print("Key", "Value") for key in sorted(D.keys()): print(key, D[key])
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
