Python Flask 解析输出中的 JSON 内容

标签 python json python-3.x parsing

我有一个输出,其中包含来自远程服务器的系统输出信息,并且在输出的最后,它包含一个 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/

相关文章:

python - 导入错误 - 发生了什么?

python - 用模拟替换对象

python - 您如何模拟 App Engine 中的用户服务?

c# - 将 JSON 字符串解析为 DynamicJsonObject 的机器特定 (??) 行为

python - 如何在 pandas 数据框中有一个空的第一行

javascript - 将平铺图 block map 读取为 JSON - 错误

javascript - 我的 javascript 适用于除 IE8 之外的所有浏览器

python-3.x - 如何排除或删除Python中的特定部分

python - 在 Windows 10 上通过 TensorFlow 安装 Keras (Python 3.5.3)

python - 将 CSV 文件导入 MongoDB 时,文件大小要求是否会发生变化?