所以這是我的 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",
}
}
}
我只需要將所有 sw_items 的名稱寫入串列 (list_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():
實作這一目標的方法是什么?
謝謝!
uj5u.com熱心網友回復:
for k,v in data.items():
for i,n in v['components'].items():
for j, l in n['sw_items'].items():
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/347897.html
