Как получить нужные значения из этого словаря?

Я пытаюсь создать карту населения, используя модуль 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

Мне удалось заставить Йемен появляться на карте, чего раньше не было, но кажется, что список не перебирает Боливию и другие страны, или, возможно, возвращенные строки не сохраняются в "коде" переменная. А вот код для визуализации файла 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 ответов

Другие вопросы по тегам