我这里有一个大的JSON文件,看起来像这样:
data = {
"Module1": {
"Description": "",
"Layer": "1",
"SourceDir": "pathModule1",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module2": {
"Description": "",
"Layer": "2",
"SourceDir": "pathModule2",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module3": {
"Description": "",
"Layer": "3",
"SourceDir": "path",
"Attributes": {
"some",
},
"Vendor": "",
},
"Module4": {
"Description": "",
"Layer": "4",
"SourceDir": "path",
"Attributes": {
"some",
}
}
}
我必须从中拿出一些东西,所以最后我得到了这个:
当供应商字段等于“comp”时,考虑该模块,考虑其SourceDir字段、所有组件及其路径和包含
因此,产出将是:
模块1,“路径模块1”,组件:[组件1,路径,[包括:包括1,包括2,包括3,包括4,包括5],[组件2,路径,包括:[包括1,包括2,包括3,包括4,包括5]]
模块2,“路径模块2”,组件:[组件1,路径,[包括:包括1,包括2,包括3,包括4,包括5],[组件2,路径,包括:[包括1,包括2,包括3,包括4,包括5]]
我真的很难访问我需要的所有字段
我目前的代码是:
with open ("DB.json", 'r') as f:
modules= json.load(f)
for k in modules.keys():
try:
if swc_list[k]["Vendor"] == "comp":
list_components.append(k)
sourceDirList.append(swc_list[k]['SourceDir'])
for i in swc_list[k]['sw_objects']:
list_sw_objects.append((swc_list[k]['sw_objects']))
except KeyError:
continue
我只获取Module1和sourceDir,而不获取Component1、2及其属性。。 我怎样才能做到这一点
谢谢
首先,我会过滤掉你不感兴趣的项目,比如:
这会删除所有您不想要的模块。这有点低效,因为您正在第二次对字典进行解析,以获得所需格式的数据,但作为第一步,更容易进行推理,这很有帮助
在这一点上,如果需要,您可以再次迭代字典-您可以得到如下结果:
要仅打印源目录和组件,可以执行以下操作:
这将给你:
编辑: 要进一步细化输出,可以将上述循环更改为:
这将给你:
相关问题 更多 >
编程相关推荐