Computers and Technology

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

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 14:30, binodkharal2048
Hi plz 11 ! when planning a table, what step comes first: "define the column headers" or "calculate the number of columns/rows"? a. calculate the number of columns/rows b. define the column headers
Answers: 1
image
Computers and Technology, 22.06.2019 19:00, sere50
Stacy works as blank. the most important soft skill she needs for this role is blank.
Answers: 3
image
Computers and Technology, 23.06.2019 01:00, EhHannuh6865
Let r be a robotic arm with a fixed base and seven links. the last joint of r is a prismatic joint, the other ones are revolute joints. give a set of parameters that determines a placement of r. what is the dimension of the configuration space resulting from your choice of parameters?
Answers: 3
image
Computers and Technology, 23.06.2019 20:30, cristalcastro901
If an appliance consumes 500 w of power and is left on for 5 hours, how much energy is used over this time period? a. 2.5 kwh b. 25 kwh c. 250 kwh d. 2500 kwh
Answers: 1
Do you know the correct 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: