python 遍历字典嵌套_Python 字典嵌套循环遍历
這是從接口獲取到的json數(shù)據(jù){
* "code":"10000",
* "charge":false,
* "remain":0,
* "msg":"查詢成功",
* "result":{
* "status":0,
* "msg":"ok",
* "result":{
* "total":1384,
* "num":5,
* "list":[
{
* "id":1,
* "classid":2,
* "name":"炸茄盒",
* "peoplenum":"3-4人",
* "preparetime":"10分鐘內(nèi)",
* "cookingtime":"10-20分鐘",
* "content":"炸茄盒外焦里嫩,入口不膩,咬上一口,淡淡的肉香和茄子的清香同時(shí)浮現(xiàn),讓人心動(dòng)不已。",
* "pic":"http://123123~~~~/recipe/upload/20160719/115137_60657.jpg",
* "tag":"健脾開(kāi)胃,兒童,減肥,宴請(qǐng),家常菜,小吃,炸,白領(lǐng),私房菜,聚會(huì)",
* "material":[
1. {
* "mname":"生粉",
* "type":0,
* "amount":"適量"
},
{...}
{...}
]
"process":[
1. {
* "pcontent":"首先我們將豬肉剁成肉泥、姜切成姜米、蔥切蔥花、蒜子切成蒜末、茄子去皮,然后在每一小段中間切一刀,但不要切斷,做成茄盒。",
* "pic":"http://123123/recipe/upload/20160719/162541_29953.jpg"
},
{[...]}
{[...]}
]
}}}}
請(qǐng)問(wèn)如何遍歷出所有字段數(shù)據(jù)了并可以插入數(shù)據(jù)庫(kù)中,我嘗試了三個(gè)for循環(huán)遍歷最后都是material[]、pcontent[]里面都是重復(fù)插入的值for x in data['result']['result']['list']:
caipu_id = x['id']
classid_id = x['classid']
name = x['name']
peoplenum = x['peoplenum'] # 人數(shù)
preparetime = x['preparetime'] # 做菜時(shí)間
content = x['content'] # 簡(jiǎn)介
pic = x['pic'] # 背景圖
tag = x['tag']
for a in x['material']:
mname_null.append(a['mname'])
amount_null.append(a['amount'])
# mname_null.clear() #清空l(shuí)ist
for (q,d) in zip(mname_null,amount_null):
data_null.append(q)
data_null.append(d)
for i in x['process']:
material_null.append(i['pcontent'])
pic_null.append(i['pic'])
for (g, k) in zip(material_null, pic_null):
process_null.append(g)
process_null.append(k)
總結(jié)
以上是生活随笔為你收集整理的python 遍历字典嵌套_Python 字典嵌套循环遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 男子购买百万路虎检出二次喷漆!路虎中国:
- 下一篇: deepin部署python开发环境_d