如何在python中使用相同的多个键:值对解析json,稍后我需要求和

如何在python中使用相同的多个键:值对解析json,稍后我需要求和,python,json,Python,Json,我需要解析JSON文件,它看起来与下面的示例相同。我需要将每个属性值与其他属性值相加,以将所有人聚集在一起,如p_30_34_yrs与其他属性p_30_34_yrs等。我需要JSON文件作为这些附加值的结果-pU30\u34\uYRS加在一起,pUtot加在一起等等。请帮助我,因为我对Python非常陌生。我可以通过load()解析它,并给我dict,但当我像dict[“features”]那样去做时,它会将它转换成list,甚至在list中,我也无法获得我应用的sum()的每个属性键值 类似这

我需要解析JSON文件,它看起来与下面的示例相同。我需要将每个属性值与其他属性值相加,以将所有人聚集在一起,如
p_30_34_yrs
与其他属性
p_30_34_yrs
等。我需要JSON文件作为这些附加值的结果-
pU30\u34\uYRS
加在一起,pUtot加在一起等等。请帮助我,因为我对Python非常陌生。我可以通过
load()
解析它,并给我
dict
,但当我像
dict[“features”]
那样去做时,它会将它转换成
list
,甚至在
list
中,我也无法获得我应用的
sum()
的每个属性键值

类似这样的东西(测试代码)


features
实际上是一个JSON数组,它变成了一个Python列表。现在还不清楚你想用它做什么,或者问题出在哪里。阅读一本关于python的书,完成一些在线教程,它会对你有所帮助。你对编程的了解太少,对其他任何东西都没有帮助。谢谢大家激励我,感谢Mika72 esp,这段代码很有效。非常感谢。不用道具,很高兴能帮助你。非常感谢您投票支持帮助解决方案。
Example:
{
"type":"..",
"box":[..],
"crs":{
..
},
"features":[
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [..]
},
      "properties": {
        "p_30_34_yrs": 421,
        "p_tot": 3210,
        "year": "2014",
        "p_75_79_yrs": 30,
        "p_40_44_yrs": 259,
        "p_55_59_yrs": 174,
        "p_65_69_yrs": 96
      },
      "id": "lga_nrp_people_2010_2014.fid--2ea3ac96_1631a637772_-4cb1"
    },
{
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          ..
     ]
      },
      "properties": {
         "p_30_34_yrs": 2316,
         "p_tot": 22690,
         "year": "2014",
         "p_75_79_yrs": 461,
         "p_40_44_yrs": 1211,
         "p_55_59_yrs": 1031,
         "p_65_69_yrs": 1071
       },
       "id": "lga_nrp_people_2010_2014.fid--2ea3ac96_1631a637772_-4cb0"
    },
and same way 6 more times.
]
}
jsn = {
    "type": "..",
    "box": "[..]",
    "features": [{
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": "[..]"
            },
            "properties": {
                "p_30_34_yrs": 421,
                "p_tot": 3210,
                "year": "2014",
                "p_75_79_yrs": 30,
                "p_40_44_yrs": 259,
                "p_55_59_yrs": 174,
                "p_65_69_yrs": 96
            },
            "id": "lga_nrp_people_2010_2014.fid--2ea3ac96_1631a637772_-4cb1"
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "p_30_34_yrs": 2316,
                "p_tot": 22690,
                "year": "2014",
                "p_75_79_yrs": 461,
                "p_40_44_yrs": 1211,
                "p_55_59_yrs": 1031,
                "p_65_69_yrs": 1071
            },
            "id": "lga_nrp_people_2010_2014.fid--2ea3ac96_1631a637772_-4cb0"
        }
    ]
}


data = jsn["features"]

props = {"p_30_34_yrs": 0,
                "p_tot": 0,
                "p_75_79_yrs": 0,
                "p_40_44_yrs": 0,
                "p_55_59_yrs": 0,
                "p_65_69_yrs": 0}

for feat in data:
    if "properties" in feat:
        if "p_30_34_yrs" in feat["properties"]: props["p_30_34_yrs"] += feat["properties"]["p_30_34_yrs"]
        if "p_tot" in feat["properties"]: props["p_tot"] += feat["properties"]["p_tot"]
        if "p_75_79_yrs" in feat["properties"]: props["p_75_79_yrs"] += feat["properties"]["p_75_79_yrs"]
        if "p_40_44_yrs" in feat["properties"]: props["p_40_44_yrs"] += feat["properties"]["p_40_44_yrs"]
        if "p_55_59_yrs" in feat["properties"]: props["p_55_59_yrs"] += feat["properties"]["p_55_59_yrs"]
        if "p_65_69_yrs" in feat["properties"]: props["p_65_69_yrs"] += feat["properties"]["p_65_69_yrs"]

print(props)