【发布时间】:2020-09-29 08:50:24
【问题描述】:
我需要解析几乎是 JSON 格式的数据流,并带有一些日期格式的借口,如下所示。
2020-09-28 15:52:13.633+0000 INFO |RestAPI.ServiceManager | Request #399: {
"context": {
"httpContextKey": 18446744071313531680,
"verbId": 2,
"verb": "GET",
"originalVerb": "GET",
"uri": "/services/v2/installation/deployments",
"protocol": "https",
"headers": {
"X-OGG-Context": "services",
"X-OGG-Service": "ServiceManager",
"X-OGG-Version": "v2",
"X-OGG-Resource": "installation/deployments",
"Content-Length": "0",
"Accept": "application/json"
},
"host": "testing-db.com",
"securityEnabled": true,
"authorization": null,
"requestId": 399,
"uriTemplate": "/services/{version}/installation/deployments",
"catalogUriTemplate": "/services/{version}/metadata-catalog/deployments"
},
"isScaRequest": true,
"content": null,
"parameters": {
"uri": {
"version": "v2"
}
}
}
Response: {
"context": {
"httpContextKey": 18446744071313531680,
"requestId": 399,
"code": "200 OK",
"headers": {
"Content-Type": "application/json",
"Expires": "0",
"Pragma": "no-cache",
"Strict-Transport-Security": "max-age=31536000;includeSubDomains"
},
"Content-Type": "application/json",
"contentType": "application/json"
},
"isScaResponse": true,
"originScaRequest": {
"context": {
"httpContextKey": 18446744071313531680,
"verbId": 2,
"verb": "GET",
"originalVerb": "GET",
"uri": "/services/v2/installation/deployments",
"protocol": "https",
"headers": {
"X-OGG-Context": "services",
"X-OGG-Service": "ServiceManager",
"X-OGG-Version": "v2",
"X-OGG-Resource": "installation/deployments",
"Content-Length": "0",
"Accept": "application/json"
},
"securityEnabled": true,
"authorization": null,
"requestId": 399
},
"isScaRequest": true
},
"content": {
"$schema": "api:standardResponse",
"links": [
{
"rel": "describedby",
"href": "https://testing-db.com/services/v2/metadata-catalog/deployments",
"mediaType": "application/schema+json"
}
],
"messages": [],
"response": {
"$schema": "ogg:installationDeployments",
"xagEnabled": false,
"deployments": [
{
"deploymentId": "39398e93-7e53-484c-9e90-3bf2f820ee73",
"deploymentName": "FOR-TMDB",
"enabled": true,
"status": "running"
},
{
"deploymentId": "30233230-94a6-4ae7-9b5e-db66105d9046",
"deploymentName": "ServiceManager",
"enabled": true,
"status": "running"
}
]
}
}
}
当 tail -0f apiserver.log | python parse 运行时,它应该解析每个 JSON 条目并获取以显示任何实体,例如 DATE 和 Request 详细信息。请注意,日志中还有 JSON 格式的 Response,需要排除。
我尝试使用下面的代码利用生成器,但问题是如何对多行进行分组,然后解析以获取 JSON 条目?因为我需要退出 for 循环来获取它,但它会流式传输失败。
import sys
def read_stdin():
readline = sys.stdin.readline()
while readline:
yield readline
readline = sys.stdin.readline()
for line in read_stdin():
print(line)
【问题讨论】:
标签: python json parsing generator