python3显示世界人口地图
这个例子是来自《Python:从入门到实践》我们在编写的时候遇到2处报错.
首先是i18n这个包,还有一处是WorldMap()函数找不到;
对于第一个解决办法是:File->Settings->Project Interpreter里单击"+"按钮,添加"pygal_maps_world"包
第二个是wm=pygal.Worldmap()报错,正确改法请参考下面源码:
import json
from pygal_maps_world.i18n import COUNTRIES
import pygal
import pygal_maps_world.maps
filename='population_data.json'
def get_country_code(country_name):
for code,name in COUNTRIES.items():
if name==country_name:
return code
with open(filename) as f:
pop_data=json.load(f)
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']))
# print(country_name+": "+ str(population))
code=get_country_code(country_name)
if code:
cc_populations[code]=population
wm=pygal_maps_world.maps.World()
wm.title='World Population in 2010,by Country'
wm.add('2010',cc_populations)
wm.render_to_file('world_population.svg')
print('countof items = ' + str(len(pop_data)) )
# for country_code in sorted(COUNTRIES.keys()):
# print(country_code,COUNTRIES[country_code])