Esempio n. 1
0
def main():
    # 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.
    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(f'{code } : {population}')
            else:
                print(f'ERROR - {country_name}')
import pygal_maps_world.maps
from countries import get_country_code
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS

# 将数据加载到一个列表中
filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)

# 创建一个包含人口数量的字典
cc_populaitons = {}
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_populaitons[code] = population

# 根据人口数量将所有国家分成三组
cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {}
for cc, pop in cc_populaitons.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_3), len(cc_pops_2), len(cc_pops_1))
Esempio n. 3
0
	def test_return_code(self):
		code = get_country_code(self.country_name[0])
		self.assertEqual(code,'cn')
		code = get_country_code(self.country_name[1])
		self.assertEqual(code,'us')
Esempio n. 4
0
	def test_return_none(self):
		code = get_country_code(self.country_name[2])
		self.assertEqual(code,None)
Esempio n. 5
0
plt.xticks(rotation=45)                # 倾斜45
plt.show()
"""

##########################################################################################
# 用世界地图可视化数据
##########################################################################################
import pygal
from pygal.maps.world import World
from countries import get_country_code
from pygal.style import RotateStyle, LightColorizedStyle

# 根据用电量的范围分组, 统计数据到{国别码: 用电量}6个字典里
group1, group2, group3, group4, group5, group6 = {}, {}, {}, {}, {}, {}
for name, used in zip(country_names, eletri_used):
    code = get_country_code(name)
    if used > 20000:
        group1[code] = used
    elif used > 8000:
        group2[code] = used
    elif used > 3000:
        group3[code] = used
    elif used > 500:
        group4[code] = used
    elif used != 0:
        group5[code] = used
    else:
        group6[code] = used

# 着色,加亮颜色主题
wm_style = RotateStyle('#336699', base_style=LightColorizedStyle)
Esempio n. 6
0
 def gccTestFunction(self):
     result = get_country_code(test_data)
     self.assertEqual("vn", result)
Esempio n. 7
0
import json
import pygal.maps.world
from pygal.style import RotateStyle as RS,LightColorizedStyle as LCS
from countries import get_country_code

file_name = 'population_data.json'
with open(file_name) as f:
    result_data = json.load(f)

cc_populations = {}

for pop_dict in result_data:
    if pop_dict['Year'] == '2010':
        country = pop_dict['Country Name']
        population = int(float(pop_dict['Value']))
        code = get_country_code(country)
        if code:
            cc_populations[code] = population

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))
Esempio n. 8
0
 def test_all_countries_code(self):
     """能够成功获取全部国家的国别码吗?"""
     code1 = get_country_code('Andorra')
     code2 = get_country_code('United Arab Emirates')
     self.assertEqual([code1, code2], ['ad', 'ae'])
Esempio n. 9
0
import json
import countries

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

for i in pop_data:
    if i['Year'] == '2009':
        country_name = i['Country Name']
        value = int(float(i['Value']))
        # print(country_name + ' : ' +value)
        # print(country_name +' : '+ str(value))
        code = countries.get_country_code(country_name)
        if code:
            print(code + ' : ' + str(value) + '\n')
        # else:
        #     print('error ' + country_name)
import json
from appscript import *
from pygal.maps.world import World
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS
from countries import get_country_code
filename = 'population_data.json'
filename_output = "three_basket_world_population"
with open(filename) as f:
    pop_data = json.load(f)
#print 2010 population of each country
cc_pop1, cc_pop2, cc_pop3 = {}, {}, {
}  #a dictionary to hold country_code - population
for data_row in pop_data:
    if (data_row['Year'] == '2010'):
        country_name = data_row['Country Name']
        country_code = get_country_code(
            country_name)  #standardize country code for each country
        population = int(
            float(data_row['Value'])
        )  #we need to convert string into float first after that int() function can remove decimal
        if country_code:
            if population < 10000000:
                cc_pop1[country_code] = population
            elif population < 1000000000:
                cc_pop2[country_code] = population
            else:
                cc_pop3[country_code] = population
            print(country_code + ": " + str(population))
        else:
            print("ERROR - No Country Code Found " + country_name)
        #print(country_name + " : " + str(population))
style_obj = RS('#0000FF', base_style=LCS)  #return an object of style