subject

Now add code to the function read_gdp_data() so that it looks up the happiness index for each country in the dictionary. If the country does not exist in the dictionary, that country should be skipped. Finally add a fourth column to your output that includes that happiness index formatted to two decimal places of precision. The first five lines of your output should look like this: Country GDP per Capita Population in Millions Happiness
China 18192.04 1394.02 5.13
United States 58592.03 332.64 6.88
India 7144.29 1326.09 3.82
Japan 43367.94 125.51 5.79
For this assignment, you do not have to write your output to a file.
When you are satisfied your program works, save and submit
happy2 is :
import csv
# function to read a tsv(tab-separared-values) file and display the re-formatted contents after removing the commas and dollar signs
def read_gdp_data():
valid_characters = '0123456789.' # string of valid characters
filename = 'world_pop_gdp. tsv' # input filename
# open the file in read mode
with open(filename) as fp:
contents = fp. readlines() # read the contents of the file into the list with each line being an element of the list
headers = contents[0].strip().split('\t') # split the first record in list contents using tab(\t) as the delimiter
# display the header
print('%-s\t%-s\t%-s'%(headers[0],h eaders[2],headers[1]))
# loop over the contents file from index 1 to end of file
for i in range(1,len(contents)):
data = contents[i].split('\t') # split the record in contents[i] using tab(\t) as the delimiter
country = data[0].strip() # extract the country information
pop = data[1].strip() # extract the population information
gdp = data[2].strip() # extract the gdp information
# loop to modify the population information so that it removes commas
mod_pop = ''
for ch in pop:
if ch in valid_characters:
mod_pop = mod_pop + ch
# loop to modify the gdp information so that it removes commas and dollar sign
mod_gdp = ''
for ch in gdp:
if ch in valid_characters:
mod_gdp = mod_gdp + ch
# display the formatted information
print('%-s\t%-s\t%-s'%(country, mod_gdp, mod_pop))
fp. close() # close the file
def make_happy_dict():
filename = 'happiness. csv' # update the file path/name here
happy_dict={}
with open(filename, 'r') as infile:
csvreader = csv. reader(infile)
for line in csvreader:
happy_dict[line[0]]=float(line[2])< br /> return happy_dict
def print_sorted_dictionary(D):
for key in sorted(D. keys()):
print(key, D[key])
def main():
# Build dictionary mapping countries to happiness index
#happy_dict = make_happy_dict()
#print_sorted_dictionary(happy_dict )
read_gdp_data() # call the read_gdp_data function
# call the main function
main()
#end of program

Data

Afghanistan,2018,2.694303274
Albania,2018,5.004402637
Algeria,2018,5.043086052
Angola,2014,3.794837952
Argentina,2018,5.792796612
Armenia,2018,5.062448502
Australia,2018,7.17699337
Austria,2018,7.396001816
Azerbaijan,2018,5.167995453
Bahrain,2017,6.227320671
Bangladesh,2018,4.499217033
Belarus,2018,5.233769894
Belgium,2018,6.89217186
Belize,2014,5.955646515
Benin,2018,5.81982708
Bhutan,2015,5.082128525
Bolivia,2018,5.915734291
Bosnia and Herzegovina,2018,5.887401104
Botswana,2018,3.4613657
Brazil,2018,6.190921783
Bulgaria,2018,5.098813534
Burkina Faso,2018,4.92723608
Burundi,2018,3.775283098

Data:

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
Germany 80.16 $52,382.96
Russia 141.72 $28,337.13
Indonesia 267.03 $12,171.08
Brazil 211.72 $15,341.31
United Kingdom 65.76 $44,479.17
France 67.85 $42,094.00
Mexico 128.65 $19,145.03
Italy 62.40 $37,129.83
Turkey 82.02 $26,652.84
South Korea 51.84 $39,259.10
Spain 50.02 $35,548.77
Saudi Arabia 34.17 $51,940.83
Canada 37.69 $47,063.09
Iran 84.92 $19,311.54
Australia 25.47 $49,005.64
Thailand 68.98 $17,918.91
Egypt 104.12 $11,563.09
Taiwan 23.60 $50,374.85
Poland 38.28 $29,413.05
Nigeria 214.03 $5,237.63

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:40
Write a program that begins by reading in a series of positive integers on a single line of input and then computes and prints the product of those integers. integers are accepted and multiplied until the user enters an integer less than 1. this final number is not part of the product. then, the program prints the product. if the first entered number is negative or 0, the program must print “bad input.” and terminate immediately. next, the program determines and prints the prime factorization of the product, listing the factors in increasing order. if a prime number is not a factor of the product, then it
Answers: 2
question
Computers and Technology, 23.06.2019 16:00
Helen is having a meeting with her colleagues in her company. they are working on the goals and objectives for the coming year. they want to ensure that these goals and objectives of the processes involved are properly evaluated. which system can helen and her colleagues apply to evaluate this? helen and her colleagues require a blank to evaluate the goals and objectives.
Answers: 2
question
Computers and Technology, 23.06.2019 18:00
Ramona enjoys her job because she is able to kids in an after school program. the work value ramona feels strongest about is a. leadership b. risk c. independence d. work with people select the best answer from the choices provided a b c d
Answers: 1
question
Computers and Technology, 23.06.2019 22:30
Lakendra finished working on her monthly report. in looking it over, she saw that it had large blocks of white space. what steps could lakendra take to reduce the amount of white space?
Answers: 3
You know the right answer?
Now add code to the function read_gdp_data() so that it looks up the happiness index for each countr...
Questions
question
Mathematics, 27.02.2021 22:00
question
Mathematics, 27.02.2021 22:00
question
Mathematics, 27.02.2021 22:00
question
French, 27.02.2021 22:00
question
Mathematics, 27.02.2021 22:00
question
Mathematics, 27.02.2021 22:00
Questions on the website: 13722361