for index, column_header inenumerate(heaeder_row): print(index, column_header)
0 AKDT
1 Max TemperatureF
2 Mean TemperatureF
3 Min TemperatureF
4 Max Dew PointF
5 MeanDew PointF
6 Min DewpointF
7 Max Humidity
8 Mean Humidity
9 Min Humidity
10 Max Sea Level PressureIn
11 Mean Sea Level PressureIn
12 Min Sea Level PressureIn
13 Max VisibilityMiles
14 Mean VisibilityMiles
15 Min VisibilityMiles
16 Max Wind SpeedMPH
17 Mean Wind SpeedMPH
18 Max Gust SpeedMPH
19 PrecipitationIn
20 CloudCover
21 Events
22 WindDirDegrees
# 打印每个国家2010年的人口数量 for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = pop_dict['Value'] print(country_name + ": " + population)
Arab World: 357868000
Caribbean small states: 6880000
East Asia & Pacific (all income levels): 2201536674
-- __snip__ --
Yemen, Rep.: 24053000
Zambia: 12927000
Zimbabwe: 12571000
# 打印每个国家2010年的人口数量 for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] # 函数float()将字符串转换为小数,而函数int()丢弃小数部分,返回一个整数 population = int(float(pop_dict['Value'])) print(country_name + ": " + str(population))
Arab World: 357868000
Caribbean small states: 6880000
East Asia & Pacific (all income levels): 2201536674
-- __snip__ --
Yemen, Rep.: 24053000
Zambia: 12927000
Zimbabwe: 12571000
获取两个字母的国别码
浅浅的使用COUNTRIES来识别国别码
1 2 3 4
from pygal_maps_world.i18n import COUNTRIES
for country_code insorted(COUNTRIES.keys()): print(country_code, COUNTRIES[country_code])
ad Andorra
ae United Arab Emirates
af Afghanistan
-- __snip__ --
zm Zambia
zw Zimbabwe
1 2 3 4 5 6 7 8 9 10 11 12 13
from pygal_maps_world.i18n import COUNTRIES
defget_country_code(country_name): """根据指定的国家,返回Pygal使用的两个字母的国别码""" for code, name in COUNTRIES.items(): if name == country_name: return code # 如果没有找到指定的国家,就返回None returnNone
print(get_country_code('Andorra')) print(get_country_code('United Arab Emirates')) print(get_country_code('Afghanistan'))
import json import pygal_maps_world.maps from IPython.display import SVG
filename = 'population_data.json' withopen(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
wm = pygal_maps_world.maps.World() wm.title = 'World Population in 2010, by Country' wm.add('2010', cc_populations)
import json import pygal_maps_world.maps from IPython.display import SVG
filename = 'population_data.json' withopen(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
# 根据人口数量将所有的国家分成三组 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 < 100000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop
wm = pygal_maps_world.maps.World() 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)
import json import pygal_maps_world.maps from pygal.style import RotateStyle from IPython.display import SVG
filename = 'population_data.json' withopen(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
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 < 100000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop
wm_style = RotateStyle('#336699') wm = pygal_maps_world.maps.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)
import json import pygal_maps_world.maps from pygal.style import LightColorizedStyle from IPython.display import SVG
filename = 'population_data.json' withopen(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
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 < 100000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop
wm = pygal_maps_world.maps.World(style = LightColorizedStyle) 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)
import json import pygal_maps_world.maps from pygal.style import LightColorizedStyle, RotateStyle from IPython.display import SVG
filename = 'population_data.json' withopen(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
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 < 100000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop
wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = pygal_maps_world.maps.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)