Python 程序未使用 JSON 信息打印



寻求帮助,以确定如何正确打印该国的国家名称和人口。 我正在使用"Python 速成课程"书中的以下代码,在书中它显示了该程序正确打印,对我来说它只是说

"------------------ (程序退出,代码:0(

按任意键继续。

任何线索为什么它无法正确打印?

import json
filename = 'population_data.json'
with open(filename) as f:
pop_data = json.load(f)
for pop_dict in pop_data:
if pop_dict['Year'] == 2010:
country_name = pop_dict['Country_Name']
population = int(float(pop_dict['Value']))
print(country_name + ": " + str(population))

我修改了一些东西。我希望这对你有所帮助。 Python scrtip

import json
filename = 'population_data.json'
with open(filename) as f:
pop_data = json.load(f)
newdic=pop_data.values()
for item in newdic: 
if item['Year'] == 2010 :
print("country name is %s population is %s "%   (item['Country_Name'],item['population']))

这是我假设的 json 文件

{
"One":{
"Year":2010,
"Country_Name":"India",
"population":78.5
},
"Two":{
"Year":2012,
"Country_Name":"XYZ",
"population":88.5
},
"Three":{
"Year":2010,
"Country_Name":"ABC",
"population":108.5
}

}

最新更新