0

I'm trying to create a population map using the pygal.maps.world module, but certain countries wouldn't show up in the final file. I appended the code with the problematic countries, but it still doesn't work. Here's the code to get the corresponding codes to each country:

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

I managed to get Yemen to appear on the map, which it didn't before, but it seems like the list doesn't iterate through Bolivia and the other countries, or possibly the returned strings don't get stored in the 'code' variable. And here's the code to render the SVG file:

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

What could be the problem?

Zekany
  • 11
  • 2
  • A dictionary would be much better than all those `elif`'s . Is the indentation of `get_country_code` a typo? I suggest you put an `else` at the end of the `elif`'s and report an error or raise an exception with the value of `country_name`. – cdarke Aug 09 '16 at 10:02
  • Yes, it was a typo, thanks for bringing it to my attention. I edited it. I'll try adding and else statement to the end of it. – Zekany Aug 09 '16 at 10:21

0 Answers0