【发布时间】:2015-12-28 17:48:04
【问题描述】:
我查看了以下一些资源:Remove python dict item from nested json file,但似乎无法让我的代码正常工作。根据我对下面我的 JSON 的理解(这是一个更长的转储的变量占位符),它是一个字典,里面有一个字典,里面有一个字典,里面有一个随机列表。我最终想看到的是我的终端的以下打印输出:
Message: [Client ID]
Link: "http://linkgoeshere.com"
这是我目前所拥有的:
ThreeLine= {u'hits': {u'hits': [{u'_id': u'THIS IS THE FIRST ONE',
u'_index': u'foo',
u'_score': None,
u'_source': {u'@timestamp': u'2015-12-21T16:59:40.000-05:00',
u'message': u'Application.INFO: [Client ID ] Information Link: http://google.com {"check1":121212} {"tags":{"sent":"15","HTML":"5661"},"person":"15651"}',
u'system': u'user-info'}},
{u'_id': u'THIS IS THE SECOND ONE',
u'_index': u'two',
u'_score': None,
u'_source': {u'@timestamp': u'2015-12-12 T16:59:40.000-05:00',
u'message': u'Application.INFO: [Client ID ] Information Link: http://google.com {"check1":565656} {"tags":{"sent":"21","HTML":"4512"},"person":"15651"}',
u'system': u'user-info'}},
]}}
unpacking= ThreeLine['hits']['hits'] #we only want to talk to the sort dictionary.
for d in unpacking:
newinfo= []
narrow=[d["_source"] for d in unpacking if "_source" in d]
narrower=[d["message"] for d in narrow if "message" in d]
newinfo.append(narrower)
print newinfo
现在,使用它的代码,它会打印两个条目,但它有很多我不关心的随机垃圾,就像所有标签一样:
{"tags":{"sent":"21","HTML":"4512"},"person":"15651"}',
那么,我如何进一步去除这些条目,以便最终得到我最终想要摆脱这种疯狂嵌套的混乱的两行?如果有人对我如何清理当前代码有任何想法,我会全力以赴并准备好学习!
【问题讨论】:
标签: python json dictionary