我有类似这样基于另一个列表
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