我有類似這樣基於另一個列表
filename = 'data.json'
with open(filename, 'r') as f:
data = json.load(f)
data.json
[{
"id":"1",
"url":"http://",
"Types:["online","offline" ],
"content:[{
"Title":"A long title"
"body":"A long body"
}],
"Other":"other1"
},
{
"id":"2",
"url":"http://2",
"Types:["online2","offline"2 ],
"content:[{
"Title":"A long title2"
"body":"A long body2"
}],
"Other":"other2"
},
.
.
.
{
"id":"2000",
"url":"http://2000",
"Types:["online2","offline2000" ],
"content:[{
"Title":"A long title200"
"body":"A long body200"
}],
"Other":"other2000"
}]
我想創建一個新的JSON文件JSON文件在JSON對象中篩選出的項目,如果ID在原來匹配我的預期ID s。
對於simplesity讓我們假設我想創建一個與原來的JSON的奇IDS一個新的JSON:
[{
"id":"1",
"url":"http://",
"Types:["online","offline" ],
"content:[{
"Title":"A long title"
"body":"A long body"
}],
"Other":"other1"
},
{
"id":"3",
"url":"http://3",
"Types:["online2","offline"3 ],
"content:[{
"Title":"A long title3"
"body":"A long body2"
}],
"Other":"other3"
},
.
.
.
{
"id":"1999",
"url":"http://1999",
"Types:["online1999","offline1999" ],
"content:[{
"Title":"A long title1999"
"body":"A long body1999"
}],
"Other":"other1999"
}]
我應該怎樣做呢?
EDIT
這是我曾嘗試,我加入我的優選的ID在陣列中。
js=[]
for i in data:
#print i
if i['id']==arr:
js.append(i)
else:
continue
你試過了什麼?添加你的代碼。 –
這很簡單。我們可以提供幫助,但我們需要一些東西。你的ID列表在哪裏?你有什麼嘗試? –
@cᴏʟᴅsᴘᴇᴇᴅ我編輯了這個問題並添加了我的方法,但它返回空數組 – Eka