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 09:50
17. implement the jvm dload instruction for the mic-2. it has a 1-byte index and pushes the local variable at this position onto the stack. then it pushes the next higher word onto the stack as well
Answers: 2
question
Computers and Technology, 23.06.2019 02:00
Read this excerpt from helen keller’s autobiography, the story of my life. have you ever been at sea in a dense fog, when it seemed as if a tangible white darkness shut you in, and the great ship, tense and anxious, groped her way toward the shore with plummet and sounding-line, and you waited with beating heart for something to happen? i was like that ship before my education began, only i was without compass or sounding-line, and had no way of knowing how near the harbour was. "light! give me light! " was the wordless cry of my soul, and the light of love shone on me in that very hour. . the morning after my teacher came she led me into her room and gave me a doll. the little blind children at the perkins institution had sent it and laura bridgman had dressed it; but i did not know this until afterward. when i had played with it a little while, miss sullivan slowly spelled into my hand the word "d-o-l-l." i was at once interested in this finger play and tried to imitate it. when i finally succeeded in making the letters correctly i was flushed with childish pleasure and pride. running downstairs to my mother i held up my hand and made the letters for doll. i did not know that i was spelling a word or even that words existed; i was simply making my fingers go in monkey-like imitation. in the days that followed i learned to spell in this uncomprehending way a great many words, among them pin, hat, cup and a few verbs like sit, stand and walk. based on this excerpt, which words best describe helen keller?
Answers: 2
question
Computers and Technology, 23.06.2019 08:30
When you interpret the behavior of others according to your experiences and understanding of the world your evaluation is
Answers: 1
question
Computers and Technology, 23.06.2019 09:00
Which best compares appointments and events in outlook 2010appointments have a subject man, and events do notappointments have a specific date or range of dates, and events do notappointments have a start and end time of day, and events do notappointments have a location option, and events do not
Answers: 2
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
History, 17.12.2020 19:20
Questions on the website: 13722367