json格式:这里
世界人口json文件,这个文件其实是一个很长的python列表,其中每个元素都是一个包含四个键的字典:国家名,国别码,年份和表示人口数量的值。
这里只关心每个国家2010年的人口数量。
import json
#将数据加载到一个列表中
filename=r'C:\Users\LPH\Desktop\pythoncrashcourse配套资源\ehmatthes-pcc-6bfeca0\chapter_16\population_data.json'
with open(filename) as f:
pop_data=json.load(f)
for pop_dict in pop_data:
if pop_dict['Year']=='2010':
country_name=pop_dict['Country Code']
#先把人数由字符串改为浮点型,再改为整型
population=int(float(pop_dict['Value']))
#在输出时再换为字符串
print(country_name+':'+str(population))
绘制世界地图时,一般用国别码表示国家。上述文件的国别码是三个字母,在pygal中是2个字母的国别码。其存储在i18n模块中,字典
COUNTRIES包含的键和值分别表示为两个字母的国别码和国家名。要查看并获取这些国别码,可以从i18n模块中导入这个字典,并打印键和值。
from pygal_maps_world.i18n import COUNTRIES
for country_code in sorted(COUNTRIES.keys()):
print(country_code,COUNTRIES[country_code])
from pygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name==country_name:
return code
#如果没有找到,就返回None
return None
import json
from pygal_maps_world.i18n import COUNTRIES
#将数据加载到一个列表中
filename=r'C:\Users\LPH\Desktop\pythoncrashcourse配套资源\ehmatthes-pcc-6bfeca0\chapter_16\population_data.json'
with open(filename) as f:
pop_data=json.load(f)
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name==country_name:
return code
#如果没有找到,就返回None
return None
for pop_dict in pop_data:
if pop_dict['Year']=='2010':
country_name=pop_dict['Country Name']
#先把人数由字符串改为浮点型,再改为整型
population=int(float(pop_dict['Value']))
#在输出时再换为字符串
code=get_country_code(country_name)
if code:
print(code+':'+str(population))
else:
print('ERROR-'+country_name)
import json
from pygal_maps_world.i18n import COUNTRIES
import pygal.maps.world as pmw
#将数据加载到一个列表中
filename=r'C:\Users\LPH\Desktop\pythoncrashcourse配套资源\ehmatthes-pcc-6bfeca0\chapter_16\population_data.json'
with open(filename) as f:
pop_data=json.load(f)
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name==country_name:
return code
#如果没有找到,就返回None
return None
#创建一个包含人口数量的字典
cc_populations={}
for pop_dict in pop_data:
if pop_dict['Year']=='2010':
country_name=pop_dict['Country Name']
#先把人数由字符串改为浮点型,再改为整型
population=int(float(pop_dict['Value']))
#在输出时再换为字符串
code=get_country_code(country_name)
if code:
cc_populations[code]=population
wm=pmw.World()
wm.title='World Population in 2010, by Country'
#方法add由2个参数,前者为标签,后者是列表/字典
wm.add('2010',cc_populations)
wm.render_to_file(r'C:\Users\LPH\Desktop\world_population.svg')
效果如图:
cc_pops_1,cc_pops_2,cc_pops_3={},{},{}
for cc,pop in cc_populations.items():
if pop<10000000:
cc_pops_1[cc]=pop
elif pop<1000000000:
cc_pops_2[cc]=pop
else:
cc_pops_3[cc]=pop
#看看每组分别包含多少个国家
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))
wm=pmw.World()
wm.title='World Population in 2010, by Country'
#方法add由2个参数,前者为标签,后者是列表/字典
wm.add('0-10m',cc_pops_1)
wm.add('10m-1bn',cc_pops_2)
wm.add('>1bn',cc_pops_3)
import json
from pygal_maps_world.i18n import COUNTRIES
import pygal.maps.world as pmw
#将数据加载到一个列表中
filename=r'C:\Users\LPH\Desktop\pythoncrashcourse配套资源\ehmatthes-pcc-6bfeca0\chapter_16\population_data.json'
with open(filename) as f:
pop_data=json.load(f)
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name==country_name:
return code
#如果没有找到,就返回None
return None
#创建一个包含人口数量的字典
cc_populations={}
for pop_dict in pop_data:
if pop_dict['Year']=='2010':
country_name=pop_dict['Country Name']
#先把人数由字符串改为浮点型,再改为整型
population=int(float(pop_dict['Value']))
#在输出时再换为字符串
code=get_country_code(country_name)
if code:
cc_populations[code]=population
cc_pops_1,cc_pops_2,cc_pops_3={},{},{}
for cc,pop in cc_populations.items():
if pop<10000000:
cc_pops_1[cc]=pop
elif pop<1000000000:
cc_pops_2[cc]=pop
else:
cc_pops_3[cc]=pop
#看看每组分别包含多少个国家
print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))
wm=pmw.World()
wm.title='World Population in 2010, by Country'
#方法add由2个参数,前者为标签,后者是列表/字典
wm.add('0-10m',cc_pops_1)
wm.add('10m-1bn',cc_pops_2)
wm.add('>1bn',cc_pops_3)
wm.render_to_file(r'C:\Users\LPH\Desktop\world_population.svg')