Python编程:从入门到实践,16章习题

16_5涵盖所有国家 :本节制作人口地图时,对于大约12个国家,程序不能自动确定其两个字母的国别码。请找出这些国家,在字典COUNTRIES 中找到它们的国别 码;然后,对于每个这样的国家,都在get_country_code() 中添加一个if-elif 代码块,以返回其国别码:

from pygal.maps.world import COUNTRIES

def get_country_code(country_name):
    """Return the Pygal 2-digit country code for the given country."""
    for code, name in COUNTRIES.items():#遍历键与值,并且以字符串保存,键为国别码,name为国家名字。
        if country_name == "Yemen":#给也门赋予国别码
            return 'ye'
        elif name == country_name:
            return code
    # If the country wasn't found, return None.
    return None

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

由于没有下载数据,没法分析。呜呜呜

16_7选择你自己的数据 :世界银行(The World Bank)提供了很多数据集,其中包含有关全球各国的信息。请访问http://data.worldbank.org/indicator/ ,并找到一个你感 兴趣的数据集。单击该数据集,再单击链接Download Data并选择CSV。你将收到三个CSV文件,其中两个包含字样Metadata,你应使用第三个CSV文件。编写一个程 序,生成一个字典,它将两个字母的Pygal国别码作为键,并将你从这个文件中选择的数据作为值。使用Worldmap 制作一个地图,在其中呈现这些数据,并根据你的 喜好设置这个地图的样式。

我下载了两个数据集一个是Agricultural land (% of land area)https://data.worldbank.org/indicator/AG.LND.AGRI.ZS?view=chart

"""16_7agricultural_land"""
import csv
from country_codes import get_country_code
import pygal_maps_world.maps
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS

#  将数据加载到一个列表中
filename = 'Agricultural_land.csv'
"""
如果在使用open函数时出现 ‘gbk‘ codec can‘t decode byte,
这种情况,就用将 with open(file) as f: 改成with open(file, ‘r’, encoding=‘utf-8’) as f:即可
"""
with open(filename, 'r', encoding='utf-8') as f:
    reader = csv.reader(f)
    header_row = next(reader)
        #  创建一个包含gdp数据的字典
    agricultural_lands = {}
    for row in reader:
        try:
            country_name = row[0]
            agricultural_land = float(row[60])#由于下载的数据为浮点数,所以要用float
        except ValueError:
            print(country_name, 'missing data')
        else:
            code = get_country_code(country_name)
            if code:
                agricultural_lands[code] = agricultural_land

#  根据数量将所有的国家分成三组
agricultural_land_1, agricultural_land_2, agricultural_land_3 = {}, {}, {}
for i, value in agricultural_lands.items():
    if value < 10:
        agricultural_land_1[i] = value
    elif value < 50:
        agricultural_land_2[i] = value
    else:
        agricultural_land_3[i] = value
    #  看看每组分别包含多少个国家
print(len(agricultural_land_1), len(agricultural_land_2), len(agricultural_land_3))
wm_style = RS('#d95e07', base_style=LCS)
wm = pygal_maps_world.maps.World(style=wm_style)
wm.title = 'Agricultural_land in 2018, by Country'
wm.add('0-10', agricultural_land_1)
wm.add('10-50', agricultural_land_2)
wm.add('>50', agricultural_land_3)
wm.render_to_file('agricultural_land.svg')

另一个是Children out of school, primary, femalehttps://data.worldbank.org/indicator/SE.PRM.UNER.FE?view=chart

"""16_7 children_number"""
import csv
from country_codes import get_country_code
import pygal_maps_world.maps
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS

#  将数据加载到一个列表中
filename = 'children_number.csv'
"""如果在使用open函数时出现 ‘gbk‘ codec can‘t decode byte,
这种情况,就用将 with open(file) as f: 改成with open(file, ‘r’, encoding=‘utf-8’) as f:即可"""
with open(filename,'r', encoding='utf-8') as f:
    reader = csv.reader(f)
    header_row = next(reader)
        #  创建一个包含gdp数据的字典
    children_numbers = {}
    for row in reader:
        try:
            country_name = row[0]
            children_number = int(row[60])
        except ValueError:
            print(country_name, 'missing data')
        else:
            code = get_country_code(country_name)
            if code:
                children_numbers[code] = children_number

#  根据数量将所有的国家分成三组
children_number_1, children_number_2, children_number_3 = {}, {}, {}
for i, value in children_numbers.items():
    if value < 1000:
        children_number_1[i] = value
    elif value < 10000:
        children_number_2[i] = value
    else:
        children_number_3[i] = value
    #  看看每组分别包含多少个国家
print(len(children_number_1), len(children_number_2), len(children_number_3))
wm_style = RS('#d95e07', base_style=LCS)
wm = pygal_maps_world.maps.World(style=wm_style)
wm.title = 'Children Out School in 2018, by Country'
wm.add('0-1000', children_number_1)
wm.add('1000-10000', children_number_2)
wm.add('>10000', children_number_3)
wm.render_to_file('children_numbers.svg')

这是我学习的过程,记录一下.

如果有需要16_7的csv数据的,联系我或者评论,我可以发给你

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值