python编程16章_[Python编程:从入门到实践] 第十六章:下载数据 习题答案

该博客介绍了如何下载并解析Open Knowledge Foundation提供的全球GDP数据集,通过Python代码展示了如何筛选2015年的GDP数据,并使用pygal库创建世界地图,将不同经济规模的国家进行分类展示。此外,还提供了country_codes模块的实现,用于查询国家名称对应的两位代码,并给出了相应的单元测试案例。
摘要由CSDN通过智能技术生成

16-6 国内生产总值:Open Knowledge Foundation 提供了一个数据集,其中包含全球各国的国内生成总值(GDP),可在http://data.okfn.org/data/core/gdp/找到这个数据集。请下载这个数据集的JSON版本,并绘制一个图标,将全球各国最近一年的GDP呈现出来。

"""我是用csv版本编的程"""

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from country_codes import get_country_code

from pygal_maps_world.maps import World

from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS

import csv

# 将数据加载到一个列表中

filename = 'gdp.csv'

with open(filename) as f:

reader = csv.reader(f)

head_row = next(reader)

di_gdp = {}

for row in reader:

try:

country_name = row[0]

year = row[2]

value = int(float(row[3]))

except ValueError:

print('missing date')

else:

# 筛选年份为2015

if year == '2015':

code = get_country_code(country_name)

if code:

di_gdp[code] = value

# 根据人口数量进行分类

di_gdp1, di_gdp10, di_gdp100 = {}, {}, {}

for co, gdp in di_gdp.items():

if gdp < 1e+11:

di_gdp1[co] = gdp

elif gdp < 1e+12:

di_gdp10[co] = gdp

else:

di_gdp100[co] = gdp

# 呈现数据

wm_style = RS('#336699', base_style=LCS)

wm = World(style=wm_style)

wm.title = 'World GDP in 2015, by Country'

wm.add('0-10b', di_gdp1)

wm.add('10b-1tn', di_gdp10)

wm.add('>1tn', di_gdp100)

wm.render_to_file('world_gdp.svg')

16-8 测试模块 country_codes:我们编写模块country_codes时,使用了print语句来核实get_country_code()能否按预期的那样工作。请利用你在第11章学到的知识,为这个函数编写合适的测试。

"""Founction get_country_code"""

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from pygal.maps.world import COUNTRIES

def get_country_code(country_name):

"""查询国家名字对应的两位代码"""

for code, name in COUNTRIES.items():

if name == country_name:

return code

elif country_name == 'Bolivia':

return 'bo'

elif country_name == 'Congo, Dem. Rep.':

return 'cd'

elif country_name == 'Congo, Rep.':

return 'cg'

elif country_name == 'Dominica':

return 'do'

elif country_name == 'Egypt, Arab Rep.':

return 'eg'

elif country_name == 'Gambia, The':

return 'gm'

elif country_name == 'Hong Kong SAR, China':

return 'hk'

elif country_name == 'Iran, Islamic Rep.':

return 'ir'

elif country_name == 'Korea, Dem. Rep.':

return 'kp'

elif country_name == 'Korea, Rep.':

return 'kr'

elif country_name == 'Kyrgyz Republic':

return 'kg'

elif country_name == 'Lao PDR':

return 'la'

elif country_name == 'Libya':

return 'ly'

elif country_name == 'Macao SAR, China':

return 'mo'

elif country_name == 'Macedonia, FYR':

return 'mk'

elif country_name == 'Moldova':

return 'md'

elif country_name == 'Slovak Republic':

return 'sk'

elif country_name == 'Tanzania':

return 'tz'

elif country_name == 'Venezuela, RB':

return 've'

elif country_name == 'Vietnam':

return 'vn'

elif country_name == 'Yemen, Rep.':

return 'ye'

# 未找到则返回None

return None

# for code, name in COUNTRIES.items():

#     print(code + ': ' + name)

# print(get_country_code('China'))

"""Test country_code"""

import unittest

from country_codes import get_country_code

class TestCountryCodes(unittest.TestCase):

"""针对get_country_codes的测试类"""

def test_country_codes_inner(self):

country_name = 'Yemen, Rep.'

code = get_country_code(country_name)

self.assertEqual(code, 'ye')

def test_country_codes_outer(self):

country_name = 'Australia'

code = get_country_code(country_name)

self.assertEqual(code, 'au')

unittest.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值