python——我正在努力学习python。为什么我得不到任何输出。?

python——我正在努力学习python。为什么我得不到任何输出。?,python,json,module,Python,Json,Module,下面是json文件的一个片段 import json import pygal #load filename into a list filename = 'population2.json' with open(filename) as f: pop_data = json.load(f) #print the 2018 population data for every country for pop_dict in pop_data: if pop_di

下面是json文件的一个片段

import json
import pygal

#load filename into a list
filename = 'population2.json'
with open(filename) as f:
    pop_data = json.load(f)

#print the 2018 population data for every country
    for pop_dict in pop_data:
        if pop_dict['Year']== '2018':
            country_name = pop_dict['Country Name']
            population = int(float(pop_dict['Value']))
            print(country_name + ":" + str(population))

我能够运行这个程序

使用以下代码行:

流行语['Year']==2018年:

与此相反:


如果流行语['Year']='2018':

如我在评论中所预期的,年份就是一个数字

[{"Country Code": "ARB", "Country Name": "Arab World", "Value": 92197753.0, "Year": 1960},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 94724510.0, "Year": 1961},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 97334442.0, "Year": 1962},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 100034179.0, "Year": 1963},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 102832760.0, "Year": 1964},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 105736431.0, "Year": 1965},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 108758610.0, "Year": 1966},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 111899364.0, "Year": 1967},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 115136178.0, "Year": 1968},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 118437195.0, "Year": 1969},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 121785650.0, "Year": 1970},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 125164745.0, "Year": 1971},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 128598734.0, "Year": 1972},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 132161298.0, "Year": 1973},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 135952252.0, "Year": 1974},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 140040649.0, "Year": 1975},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 144453278.0, "Year": 1976},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 149161891.0, "Year": 1977},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 154111175.0, "Year": 1978},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 159218552.0, "Year": 1979},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 164420785.0, "Year": 1980},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 169698994.0, "Year": 1981},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 175061792.0, "Year": 1982},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 180505996.0, "Year": 1983},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 186035305.0, "Year": 1984},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 191650331.0, "Year": 1985},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 197338142.0, "Year": 1986},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 203084971.0, "Year": 1987},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 208889682.0, "Year": 1988},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 214753970.0, "Year": 1989},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 222653373.0, "Year": 1990},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 228731685.0, "Year": 1991},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 232956352.0, "Year": 1992},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 239243261.0, "Year": 1993},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 245449409.0, "Year": 1994},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 253107289.0, "Year": 1995},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 259000953.0, "Year": 1996},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 264822188.0, "Year": 1997},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 270575803.0, "Year": 1998},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 276393837.0, "Year": 1999},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 282344154.0, "Year": 2000},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 288432163.0, "Year": 2001},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 294665185.0, "Year": 2002},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 301113849.0, "Year": 2003},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 307862843.0, "Year": 2004},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 314965793.0, "Year": 2005},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 322452754.0, "Year": 2006},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 330290716.0, "Year": 2007},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 338395961.0, "Year": 2008},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 346629220.0, "Year": 2009},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 354890042.0, "Year": 2010},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 363158703.0, "Year": 2011},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 371443547.0, "Year": 2012},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 379705719.0, "Year": 2013},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 387907748.0, "Year": 2014},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 396028278.0, "Year": 2015},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 404024433.0, "Year": 2016},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 411898965.0, "Year": 2017},{"Country Code": "ARB", "Country Name": "Arab World", "Value": 419790588.0, "Year": 2018},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4194710.0, "Year": 1960},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4274060.0, "Year": 1961},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4353628.0, "Year": 1962},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4432217.0, "Year": 1963},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4508198.0, "Year": 1964},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4580374.0, "Year": 1965},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4648367.0, "Year": 1966},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4712526.0, "Year": 1967},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4773902.0, "Year": 1968},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4833842.0, "Year": 1969},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4893454.0, "Year": 1970},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 4953087.0, "Year": 1971},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5012612.0, "Year": 1972},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5071954.0, "Year": 1973},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5130833.0, "Year": 1974},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5189153.0, "Year": 1975},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5246563.0, "Year": 1976},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5303307.0, "Year": 1977},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5360561.0, "Year": 1978},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5419884.0, "Year": 1979},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5482206.0, "Year": 1980},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5548512.0, "Year": 1981},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5617885.0, "Year": 1982},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5687092.0, "Year": 1983},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5751543.0, "Year": 1984},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5808179.0, "Year": 1985},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5855443.0, "Year": 1986},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5894827.0, "Year": 1987},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5930179.0, "Year": 1988},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 5967018.0, "Year": 1989},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 6009251.0, "Year": 1990},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 6058308.0, "Year": 1991},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 6112815.0, "Year": 1992},{"Country Code": "CSS", "Country Name": "Caribbean small states", "Value": 6170351.0, "Year": 1993},

请,您可以将您的代码标记为代码并添加预期的输出。请添加示例输入JSON文件。从您显示的情况来看,
if
块中的条件不是
True
,并且它从不执行主体。如果我不得不猜测-可能
2018
是一个数字,而不是
str
pop_data是一个键为字符串、值为字典的字典吗?如果是这样,您需要将for循环修改为pop_data.values()中的pop_dict的
或者,如果您想同时获取键和值,您可以对pop_data.items()中的k,v执行类似
的操作:
在pop_数据中为pop_dict执行
操作将只返回字典的键,而不返回值。预期输出是一系列国家及其人口在2018年使用了我使用的JSON文件,并删除了“:”,并且一直有效。现在我想要一个特定的年份。TIAYes特定的年份与您发送的文件一起工作。这里似乎不起作用。我刚刚更改了以下行:if pop_dict['year']='2018':使用2018而不是'2018'
import json
import pygal

#load filename into a list
filename = 'population2.json'
with open(filename) as f:
    pop_data = json.load(f)
# print the 2018 population data for every country
for pop_dict in pop_data:
    if pop_dict['Year'] == 2018:
        print(f"{pop_dict['Country Name']}:{pop_dict['Value']:.0f}")

        # or
        country = pop_dict['Country Name']
        population = pop_dict['Value']
        print(f"{country}:{population:.0f}")