在 python 中加载 json 时没有数据
no data when loading json in pyhon
我只想像这样打印 json 文件
import json
from country_codes import get_country_code
filename = 'gdp.json'
with open(filename) as f:
pop_data = json.load(f)
cc_populations = {}
for pop_dict in pop_data:
if pop_dict['Year'] == '1970':
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)
json 文件是 here
运行时,什么也没有出现
你JSON中的年份是整数。
将if pop_dict['Year'] == '1970':
更改为if pop_dict['Year'] == 1970:
工作示例:
pop_data = [{"Country Code": "ARB", "Country Name": "Arab World", "Value": 25760683041.0857, "Year": 1968},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 28434203615.4829, "Year": 1969},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 31385499664.0672, "Year": 1970},]
import json
cc_populations = {}
for pop_dict in pop_data:
if pop_dict['Year'] == 1970:
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
code = (country_name)
if code:
print(code + ": " + str(population))
else:
print('ERROR - ' + country_name)
输出:
Arab World: 31385499664
旁注:Value
已经是一个浮点数,因此不需要转换 float(pop_dict['Value'])
。
我只想像这样打印 json 文件
import json
from country_codes import get_country_code
filename = 'gdp.json'
with open(filename) as f:
pop_data = json.load(f)
cc_populations = {}
for pop_dict in pop_data:
if pop_dict['Year'] == '1970':
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)
json 文件是 here
运行时,什么也没有出现
你JSON中的年份是整数。
将if pop_dict['Year'] == '1970':
更改为if pop_dict['Year'] == 1970:
工作示例:
pop_data = [{"Country Code": "ARB", "Country Name": "Arab World", "Value": 25760683041.0857, "Year": 1968},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 28434203615.4829, "Year": 1969},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 31385499664.0672, "Year": 1970},]
import json
cc_populations = {}
for pop_dict in pop_data:
if pop_dict['Year'] == 1970:
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
code = (country_name)
if code:
print(code + ": " + str(population))
else:
print('ERROR - ' + country_name)
输出:
Arab World: 31385499664
旁注:Value
已经是一个浮点数,因此不需要转换 float(pop_dict['Value'])
。