我有一个输出,其中包含来自远程服务器的系统输出信息,并且在输出的最后,它包含一个 JSON 对象。像这样:
输出:
...
-Verification-
-Test1: PASS
-Test2: PASS
...
...
Result: PASS
...
#JSON Object below
{"path": "/my/path/file.log", "name": "Roger", "year": 2018}
输出仅包含一组 {}
,这就是我要解析的 JSON 对象。所以我们不用担心抓取到错误的信息
我希望我的 Flask 应用解析此输出以仅返回 JSON 对象({}
中的所有内容,包括 {}
)并过滤上面的所有文本它。我怎样才能做到这一点?
最佳答案
import re
jsonstr='''...
-Verification-
-Test1: PASS
-Test2: PASS
...
...
Result: PASS
...
#JSON Object below
{"path": "/my/path/file.log", "name": "Roger", "year": 2018}'''
print(jsonstr.lstrip(re.sub('{.*}','',jsonstr)))
jsonstr='''...
-Verification-
-Test1: PASS
-Test2: PASS
...
...
Result: PASS
...
#JSON Object below
{"path": "/my/path/file.log", "name": "Roger", "year": 2018}'''
print([i for i in jsonstr.splitlines() if i.startswith('{') and i.endswith('}')])
关于Python Flask 解析输出中的 JSON 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412578/