world_population.py:
"""
Author: yeahthon
Date : 2019-08-13 15:58:38
E-mail: [email protected]
"""
import json
from country_codes import get_country_code
#load data into list
filename = 'population_data.json'
with open(filename) as f:
pop_data = json.load(f)
#print every country's population in 2010
#transform str into int
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)
country_codes.py
"""
Author: yeahthon
Date : 2019-08-13 16:23:19
E-mail: [email protected]
"""
from pygal.i18n import COUNTRIES
def get_country_code(country_name):
"""return country code in tow character according appointed country"""
for code, name in COUNTRIES.items():
if name == country_name:
return code
#if appointed country is not existed, return None
return None
americas.py
"""
Author: yeahthon
Date : 2019-08-13 16:48:09
E-mail: [email protected]
"""
import pygal
wm = pygal.Worldmap()
wm.title = 'North, Central, and South America'
wm.add('North America', ['ca', 'mx', 'us'])
wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('South America', ['cr', 'bo', 'br', 'cl', 'co', 'ec', 'gf',
'gy', 'pe', 'py', 'sr', 'uy', 've'])
wm.render_to_file('americas.svg')
Debug country_codes.py显示:
G:\python\practice\pycharm\venv\Scripts\python.exe G:/python/pycharm/2019081301/country_codes.py
File "G:/python/pycharm/2019081301/country_codes.py", line 7
import pygal.i18n import COUNTRIES
^
SyntaxError: invalid syntax
Process finished with exit code 1
异常分析:pygal.i18n不能被识别,导致符号异常
异常处理:在python3 中,pygal.i18n已经被pygal_maps_world.i18n 所替代,所以找到pygal_maps_world模块并加载,并修改代码如下即可
country_codes.py
"""
Author: yeahthon
Date : 2019-08-13 16:23:19
E-mail: [email protected]
"""
import pygal
import pygal_maps_world
from pygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
"""return country code in tow character according appointed country"""
for code, name in COUNTRIES.items():
if name == country_name:
return code
#if appointed country is not existed, return None
return None
Debug americas.py显示
G:\python\practice\pycharm\venv\Scripts\python.exe G:/python/pycharm/2019081301/americas.py
Traceback (most recent call last):
File "G:/python/pycharm/2019081301/americas.py", line 9, in
wm = pygal.Worldmap()
AttributeError: module 'pygal' has no attribute 'Worldmap'
Process finished with exit code 1
异常分析:python3找不到模块 ‘woeldmap’
异常分析:跟上一个异常类似,修改代码如下即可
"""
Author: yeahthon
Date : 2019-08-13 16:48:09
E-mail: [email protected]
"""
import pygal
import pygal_maps_world.maps
wm = pygal_maps_world.maps.World()
wm.title = 'North, Central, and South America'
wm.add('North America', ['ca', 'mx', 'us'])
wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('South America', ['cr', 'bo', 'br', 'cl', 'co', 'ec', 'gf',
'gy', 'pe', 'py', 'sr', 'uy', 've'])
wm.render_to_file('americas.svg')