[英]Remove Python dict from JSON file response
我查看了一些如下资源: 从嵌套的json文件中删除python dict项目,但似乎无法使我的代码正常工作。 据我对下面的JSON的理解(这是WAY较长转储的变量占位符),它是一个dict,其中有一个dict,其中有一个dict,其中带有...。 我最终希望看到的是以下打印输出到我的终端:
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"}',
那么,如何进一步去除这些条目,以使我最终想摆脱这两个疯狂嵌套的混乱局面呢? 如果有人对如何清理当前代码有任何想法,我会全力以赴,随时准备学习!
“标签”字典不是字典。 它是消息字符串中嵌入的文本 :
>>> ThreeLine['hits']['hits'][0]['_source']['message']
u'Application.INFO: [Client ID ] Information Link: http://google.com {"check1":121212} {"tags":{"sent":"15","HTML":"5661"},"person":"15651"}'
您必须进行一些字符串解析才能删除它。 您可以使用正则表达式:
import re
id_and_link = re.compile(r'(\[[^]]+\]) Information Link: (https?://[\w\d/.]+)')
messages = (entry['_source']['message'] for entry in ThreeLine['hits']['hits'] if '_source' in entry and 'message' in entry['_source'])
for message in messages:
match = id_and_link.search(message)
if not match:
continue
id_, link = match.groups()
print 'Message:', id_
print 'Link:', link
print
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.