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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 10:30, araminaara691
How can a user open a blank presentation? 1.on the file menu, click new, and then click recent templates 2.on the file menu, click new, and then click blank presentation 3. on the view menu, click templates, and then click recent templates 4. on the view menu, click samples, and then click blank presentation
Answers: 1
image
Computers and Technology, 22.06.2019 22:20, gingerham1
Avariable of the data type arrays is storing 10 quantities. what is true about these quantities? a. the quantities all have different characteristics. b. the quantities all have the same characteristics. c. five quantities have the same and five have different characteristics. d. it is necessary for all quantities to be integers. e. it is necessary for all quantities to be characters.
Answers: 2
image
Computers and Technology, 23.06.2019 07:30, cireland
Write a program that inserts the digits of an integer into an array in originalorderfollowed by reverse order. first, promptthe user to enter a positive integer(> 0). determine the number of digits of the integer. create a dynamically allocated integer arrayof a size twice the number of digits. now insert the digits in original order which will occupy half of the array. then, insert the digits in reverse order. finally, output thedigits in thearray. use at least two functions to organize your program.
Answers: 3
image
Computers and Technology, 23.06.2019 14:30, soapai
Select the correct answer. sean is a computer programmer. he has programmed an application for toddlers that plays nursery rhymes. however, a logic error has occurred in the program. which problem is a likely consequence of the error? a. the program crashes every time the user wants to play the nursery rhymes. b. the program crosses its buffer boundaries and overwrites an adjacent program. c. the program plays a different nursery rhyme than the one the user intended to play. d. the program shows different structures in its programming language code. e. the program introduces new viruses every time the user plays a nursery rhyme.
Answers: 1
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 in other subjects: