Example #1
0
def v2():
    filename = 'population_data.json'
    with open(filename) as f:
        pop_data = json.load(f)

    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
            else:
                print('ERROR - ' + country_name)

    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

    print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))

    wm_style = RotateStyle('#336699', base_style=LightColorizedStyle)
    wm = pygal.maps.world.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_tmp.svg')
Example #2
0
def v1():
    # 将数据加载到一个列表中
    filename = 'population_data.json'
    with open(filename) as f:
        pop_data = json.load(f)

    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)
Example #3
0
def load_world_data():

    filename = 'population_data.json'
    with open(filename) as f:
        pop_data = json.load(f)

    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
            else:
                print('ERROR - ' + country_name)
    return cc_populations
Example #4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
        pass


    filename = 'population_data.json'
    with open(filename) as file:
        pop_data = json.load(file)

    # Build a dictionary of population data
    cc_populations = {}
    for pop_dict in pop_data:
        if '2010' == pop_dict['Year']:
            country_name = pop_dict['Country Name']
            population = int(float(pop_dict['Value']) + 0.5)  # change the string such as '12345.6789' to float, and than change it to int
            code = get_country_code(country_name)

            if code:
                cc_populations[code] = population


    # Group the countries into three population levels.
    cc_pop_1, cc_pop_2, cc_pop_3 = {}, {}, {}
    for cc, pop in cc_populations.items():
        if pop < 1e7:
            cc_pop_1[cc] = pop
        elif pop < 1e9:
            cc_pop_2[cc] = pop
        else:
            cc_pop_3[cc] = pop


    world_map_style = RotateStyle('#336699')
    world_map = pygal.maps.world.World(style=world_map_style)
    world_map.title = "World Population in 2010, by Country"
    world_map.add('< 10^7', cc_pop_1)
    world_map.add('10^7 ~ 10^9', cc_pop_2)
    world_map.add('> 10^9', cc_pop_3)

    world_map.render_to_file('world_population.svg')


    return 0
Example #5
0
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS
from pygal.maps.world import World

from country_code import get_country_code
#load the data into a list
filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)

#Print the 2010 population for each country.
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 the 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))
import json
from country_code import get_country_code
from pygal.maps.world import World
from pygal.style import RotateStyle, LightColorizedStyle

filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)

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

cc1, cc2, cc3 = {}, {}, {}
for cc, popu in cc_populations.items():
    if popu < 10000000:
        cc1[cc] = popu
    elif popu < 1000000000:
        cc2[cc] = popu
    else:
        cc3[cc] = popu

wm_style = RotateStyle('#336699', base_style=LightColorizedStyle)
wm = World(style=wm_style)
wm.title = 'Population in world'
wm.add('< 10m', cc1)
Example #7
0
import json
import pygal
from country_code import get_country_code

filename = 'cpi.json'

with open(filename) as f:
    jurisdiction_cpi = {}
    cpi_data = json.load(f)  # Store the file in the cpi_data object.
    for cpi_dict in cpi_data:  # Iterate each dictionary set in the JSON file.
        jurisdiction_name = cpi_dict[
            'Jurisdiction']  # Assign the country name (jurisdiction) to jurisdiction_name
        cpi = cpi_dict['2015']  # Query the info in 2015.
        if cpi != '-':  # Some data is unavailable and marked as "-" in the JSON file. We will ignore these cases.
            cpi = int(cpi)  # Convert the CPI string to integers.
        code = get_country_code(
            jurisdiction_name)  # Get the country code of the jurisdiction.
        if code and cpi != '-':
            jurisdiction_cpi[code] = cpi  # Store the CPI for a jurisdiction.
    # The output is dict_items([('dk', 91), ('nz', 91), ('fi', 90), ('se', 89),...)
    print(jurisdiction_cpi.items())

jurisdiction_cpi_1, jurisdiction_cpi_2, jurisdiction_cpi_3 = {}, {}, {
}  # Define three new dictionaries to group the countries.

for cc, cpi_value in jurisdiction_cpi.items(
):  # cc is the key and cpi_value is the value
    if cpi_value < 40:
        jurisdiction_cpi_1[cc] = cpi_value
    elif cpi_value < 60:
        jurisdiction_cpi_2[cc] = cpi_value
    else:
Example #8
0
import json
import pygal
from country_code import get_country_code
from pygal.style import CleanStyle as RBS

# Список заполяется данными
filename = '.\\data\\gdp_json.json'
with open(filename) as f:
    gdp_data = json.load(f)

# Построение словаря с данными численности населения
cc_gdps = {}
for gdp_dict in gdp_data:
    if gdp_dict['Year'] == 2015:
        country = gdp_dict['Country Name']
        gdp = int(gdp_dict['Value'])
        code = get_country_code(country)
        if code:
            cc_gdps[code] = gdp

wm_style = RBS()
wm = pygal.maps.world.World(style=wm_style)
wm.title = 'World GDP in 2010, by Country'
wm.add('2015', cc_gdps)

wm.render_in_browser()
Example #9
0
 def test_country_code(self):
     country_name = 'Yemen,Rep.'
     country_code = 'ye'
     code = get_country_code(country_name)
     self.assertEqual(code, country_code)