如何从这本字典中获取所需的值?

时间:2016-08-09 09:50:50

标签: python python-3.x svg pygal

我尝试使用pygal.maps.world模块创建人口地图,但某些国家/地区不会显示在最终文件中。我在有问题的国家/地区附加了代码,但它仍然无效。以下是为每个国家/地区提供相应代码的代码:

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():
        if name == country_name:
            return code
        elif country_name == 'Yemen, Rep.':
            return 'ye'
        elif country_name == 'Bolivia, Plurinational State of':
            return 'bo'
        elif country_name == 'United Arab Emirates':
            return 'ae'
        elif country_name == 'Bosnia and Herzegovina':
            return 'ba'
        elif country_name == 'Brunei Darussalam':
            return 'bn'
        elif country_name == 'Congo, the Democratic Republic of the':
            return 'cd'
        elif country_name == 'Central African Republic':
            return 'cf'
        elif country_name == 'Iran, Islamic Republic of':
            return 'ir'
        elif country_name == 'Korea, Democratic People\'s Republic of':
            return 'kp'
        elif country_name == 'Korea, Republic of':
            return 'kr'
        elif country_name == 'Lao People\'s Democratic Republic':
            return 'la'
        elif country_name == 'Lybian Arab Jamahiriya':
            return 'ly'
        elif country_name =='Macedonia, the former Yugoslav Republic of':
            return 'mk'
        elif country_name == 'Moldova, Republic of':
            return 'md'
        elif country_name == 'Palestine, State of':
            return 'ps'
        elif country_name == 'Saint Helena, Ascension and Tristan da Cunha':
            return 'sh'
        elif country_name == 'Taiwan, Province of China':
            return 'tw'
        elif country_name == 'Tanzania, United Republic of':
            return 'tz'
        elif country_name == 'Venezuela, Bolivarian Republic of':
            return 've'
    # If the country wasn't found, return None.
    return None

我设法让也门出现在地图上,它之前没有出现过,但看起来这个名单并没有在玻利维亚和其他国家进行迭代,或者可能是返回的字符串不是&#39 ; t存储在'代码'变量。这里是渲染SVG文件的代码:

import pygal
from pygal.maps.world import World
from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS
from country_codes import get_country_code
# Load the data into a list.
filename ="population_data.json"
with open(filename) as f:
    pop_data = json.load(f)    
# Build a dictionary of population data.
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

# Group countries into 3 population levels.
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

# See how many countries are in each level.
print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))

wm_style = RS('#336699', base_style=LCS)            
wm = World(style=wm_style)
wm.title = 'World Population in 2010, by Country'
wm.add('0-10m', cc_pops_1)
wm.add('10m-1bn', cc_pops_2)
wm.add('>1bn', cc_pops_3)

wm.render_to_file('world_population.svg')

可能是什么问题?

0 个答案:

没有答案