Python异常处理:SyntaxError: invalid syntax import pygal.i18n import COUNTRIES

world_population.py:

"""
Author: yeahthon
Date  : 2019-08-13  15:58:38
E-mail: yeahthon@163.com
"""

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: yeahthon@163.com
"""

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: yeahthon@163.com
"""

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: yeahthon@163.com
"""

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 <module>
    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: yeahthon@163.com
"""

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')

得到运行结果包含图表的 .svg文件,使用浏览器打开如下图所示
americas.svg

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值