[英]Extracting data from nested dictionary JSON file
我正在嘗試訪問第一個字典的鍵,並且僅從列表中訪問red
以將它們放入多維數組中。
{
"100": {
"pass": "edrsf45tf"
"colors": ["red", "blue"]
},
"101": {
"pass": "trf56trt"
"colors": ["red", "black", "white"]
}
}
y_train = []
with open('/data.json') as data:
accessData = json.load(data)
for i in range(len(accessData)):
y_train.append(acessData[i]['colors']
我被困在這里,得到了這個想要的輸出:
id color
100 red
101 red
102 null
... ...
300 red
加載 JSON 並將其保存到字典后,如果紅色是顏色鍵中的第一個,則可以這樣做:
jdct = { "100": { "pass": "edrsf45tf", "colors" :["red", "blue" ] },
"101": { "pass": "trf56trt", "colors": ["red", "black", "white" ] } }
for k,v in jdct.items():
print(k, v["colors"][0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.