【问题标题】:Python parse nested JSON file and take out needed attributesPython 解析嵌套的 JSON 文件并取出所需的属性
【发布时间】:2021-12-13 14:50:33
【问题描述】:

这是我的 JSON 文件:

data = {
        "Module1": {
            "Description": "",
            "Layer": "1",
            "SourceDir": "pathModule1",
            "Attributes": {
                "some",
            },
            "Vendor": "comp",
            "components":{
                "Component1": {
                   "path": "pathToCom1",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1:"{
                       ...(something)
                       }
                      "sw_item2:"{
                       ...(something)
                       }
                      "sw_item3:"{
                       ...(something)
                       }
                    }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                },
                "Component2":{
                   "path": "pathToCom2",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1_2:"{
                       ...(something)
                       }
                      "sw_item2_2:"{
                       ...(something)
                       }
                      "sw_item3_3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                }
            }
        },
        "Module2": {
            "Description": "",
            "Layer": "2",
            "SourceDir": "pathModule2",
            "Attributes": {
                "some",
            },
            "Vendor": "comp",
            "components":{
                "Component1": {
                   "path": "pathToCom1",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1:"{
                       ...(something)
                       }
                      "sw_item2:"{
                       ...(something)
                       }
                      "sw_item3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                },
                "Component2":{
                   "path": "pathToCom2",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1_2:"{
                       ...(something)
                       }
                      "sw_item2_2:"{
                       ...(something)
                       }
                      "sw_item3_3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                }
            }
        },
        "Module3": {
            "Description": "",
            "Layer": "3",
            "SourceDir": "path",
            "Attributes": {
                "some",
            },
            "Vendor": "",
        },
        "Module4": {
            "Description": "",
            "Layer": "4",
            "SourceDir": "path",
            "Attributes": {
                "some",
            }
        }
    }

我只需要在列表 (list_sw_items) 中写入所有 sw_items 的名称。

所以最后 list_sw_items = [sw_item1, sw_item2, sw_item3, sw_item1_2, sw_item2_2, sw_item3_2, sw_item1, sw_item2 , sw_item3 ]

我现在的代码,我在 sw_items 上不断收到错误 (KeyError)

for k,v in data.items():
    for i,n in v['sw_items'].items():

有什么方法可以做到这一点?

谢谢!

【问题讨论】:

  • 它嵌套在 Module1 和 components 内,这就是为什么您没有足够深的索引,只需仔细观察您的数据结构并以这种方式构建您的逻辑,您就走在了正确的轨道上跨度>
  • 我知道但仍然无法访问它
  • 知道了,会写答案

标签: python json parsing attributes


【解决方案1】:
for k,v in data.items():
   for i,n in v['components'].items():
        for j, l in n['sw_items'].items():

【讨论】:

  • 请附上解释
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 2021-08-27
  • 2019-03-31
  • 1970-01-01
  • 2020-01-18
相关资源
最近更新 更多