在python中,有没有一种方法可以提取嵌入的json字符串? [英] In python, is there a way to extract a embedded json string?

查看:101
本文介绍了在python中,有没有一种方法可以提取嵌入的json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在解析一个带有一些嵌入式json的非常大的日志文件.

So I'm parsing a really big log file with some embedded json.

所以我会看到这样的行

foo="{my_object:foo, bar:baz}" a=b c=d

问题是,由于内部json可以有空格,但是在JSON之外,空格充当元组定界符(除非它们具有未加引号的字符串.对于任何傻瓜来说,Huzzah都是个好主意),我不是确保在不重新实现JSON解析器大部分的情况下,如何确定JSON字符串的末尾在哪里.

The problem is that since the internal json can have spaces, but outside of the JSON, spaces act as tuple delimiters (except where they have unquoted strings . Huzzah for whatever idiot thought that was a good idea), I'm not sure how to figure out where the end of the JSON string is without reimplementing large portions of a json parser.

是否有用于Python的json解析器,我可以给它'{"my_object":"foo", "bar":"baz"} asdfasdf',并且它可以返回({'my_object' : 'foo', 'bar':'baz'}, 'asdfasdf'),还是我必须手动重新实现json解析器?

Is there a json parser for Python where I can give it '{"my_object":"foo", "bar":"baz"} asdfasdf', and it can return ({'my_object' : 'foo', 'bar':'baz'}, 'asdfasdf') or am I going to have to reimplement the json parser by hand?

推荐答案

找到了一个非常酷的答案.使用json.JSONDecoder的scan_once函数

Found a really cool answer. Use json.JSONDecoder's scan_once function

In [30]: import json

In [31]: d = json.JSONDecoder()

In [32]: my_string = 'key="{"foo":"bar"}"more_gibberish'

In [33]: d.scan_once(my_string, 5)
Out[33]: ({u'foo': u'bar'}, 18)

In [37]: my_string[18:]
Out[37]: '"more_gibberish'

请小心

In [38]: d.scan_once(my_string, 6)
Out[38]: (u'foo', 11)

这篇关于在python中,有没有一种方法可以提取嵌入的json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆