Python对Json格式的文件进行操作
Python对Json格式的基本操作
制作世界人口地图
1提取相关数据
1.1查询数据
importjson
filename = 'population_data.json'
with open(filename) as f:
pop_data = json.load(f) #将数据转换成Python能够处理的格式
for pop_dict in pop_data: #遍历pop_data数据,每个字典依次存放在pip_dict里面
#打印每个国家2010年的人口数量
if pop_dict['Year'] == '2010':
country_name = pop_dict['CountryName']
population = pop_dict['Value']
print(country_name+':'+population)
1.2将字符串转换成数字值
population =int(float(pop_dict['Value']))
在这一行转换数据类型
1.3获取两个字母的国别码:
书上又出现一个坑,我们下载pygal_maps_world来导入COUNTRIES包
from pygal_maps_world.i18n import COUNTRIES
for country_code in sorted(COUNTRIES.keys()):
print(country_code,COUNTRIES[country_code])
这样可以查看所以国家的国别码
为了获取国别码,我们编写一个寒素,在COUNTRIES中查找并返回国别码
frompygal_maps_world.i18n import COUNTRIES
def get_country_code(country_name):
for code, name in COUNTRIES.items():
if name == country_name:
return code
return None
'''使用get_country_code()接收国名,将其存储在country_name中,遍历COUNTRIES中的国家名-国别码对应,返回国别码'''
print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))
1.4制作世界地图
开始正事了
有了国别码,就可以做地图了。
这里有个坑,书上的pygal.i1n8是不行的,所以我导入maps.world
先做个美洲人口地图显示看看:
frompygal.maps.world import World
wm = World() #创建一个空的世界
wm.force_uri_protocol = 'http' #要加上这个,不然会报错
wm.title = 'The distrubtion of population in the world ’
wm.add('North America', ['ca', 'mx', 'us']) #添加图例名字和国家信息
wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe','py', 'sr', 'uy', 've'])
# wm.add('West Asia',['af','lb','ye','tj','tk','kz','iq','ir'])
# wm.add('East Asia',['cn','kr','kq','jp'])
wm.render_to_file('americas.svg') #保存文件
下面是世界人口分布,我加了两个数量级的图例,能够清楚的区分人口数量
from pygal.maps.world import World
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS
from country_codes import get_country_code
filename = 'population_data.json'
with open(filename) as f:
pop_data = json.load(f)
#创建一个人口数量的字典
c_populations = {}
for pop_dict in pop_data:
if pop_dict['Year'] == '2010':
country_name = pop_dict['CountryName']
population =int(float(pop_dict['Value']))
code =get_country_code(country_name)
if code:
c_populations [code] =population
#将人口等级分为三组
c_pops_0, c_pops_1, c_pops_2, c_pops_3, c_pops_4 ,c_pops_5 ,c_pops_6= {}, {},{}, {},{},{},{}
for c, pop in c_populations.items():
if pop <= 10000:
c_pops_0[c] = pop
elif pop <= 500000:
c_pops_1[c] = pop
elif pop <= 5000000:
c_pops_2[c] = pop
elif pop <= 100000000:
c_pops_3[c] = pop
elif pop <= 100000000:
c_pops_4[c] = pop
elif pop <= 3000:
c_pops_6[c] = pop
else :
c_pops_5[c] = pop
#创建6个字典存放不同数量级的国家地区
print(len(c_pops_0), len(c_pops_1), len(c_pops_2), len(c_pops_3),len(c_pops_4)), len(c_pops_5)
wm_style = RS('#336699', base_style=LCS) #添加颜色
wm = World(style=wm_style) #RotateStyle是个颜色选择器,返回一个样式对象传给World的6个组级
#wm = World()
wm.force_uri_protocol = 'http'
wm.title = '2010年世界人口分布地图'
# wm.add('',['kr','kq','ir','eg','am','lr']) #可以把没有显示的地区加上去
wm.add('小于3000',c_pops_6)
wm.add('3000到一万',c_pops_0)
wm.add('1万到50万', c_pops_1)
wm.add('50万到5000万', c_pops_2)
wm.add('5000万到1亿', c_pops_3)
wm.add('1亿到10亿',c_pops_4)
wm.add('10亿以上',c_pops_5)
wm.render_to_file('world_population.svg')