import json
from countries_code import get_country_code
# 将数据加载到一个列表当中:
filename='population_json.json'
with open(filename) as f:
pop_date=json.load(f)
# 打印每个国家人口数量:
for pop_dict in pop_date:
if pop_dict['Year']=='2010':
country_name=pop_dict['Country Name']
population=int(float(pop_dict['Value'])) # 在python中不能直接将浮点型转换成为整数型,因此需要先将字符串类型数据先转换成为浮点型数据,然后再转换成为整数型数据。
code=get_country_code(country_name)
if code:
print(code + ':' + str(population))
else:
print('ERROR-' + country_name) # 数据的连接类型必须要相同类型才能进行连接,因此需要字符串类型才能与字符串类型相连接。