我试图在"Tags“键下的"process:ilapd”值中捕获"ilapd“字符串,但没有成功。我怎么才能抓住这根绳子?
我尝试用for循环中的几个变量来迭代数据,但是对于类型整数,我一直在获取错误。
JSON数据如下:
data = {
"alertOwner":"team",
"assignGroup":"team",
"component":"lnx2",
"Tags":"application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description":"Process ilapd is not running on lnx2, expected state is running,",
"Event Url":"https://app.datadoghq.com/monitors#67856691",
"logicalName":"lnx2",
"Metric Graph":"<img src=\"\" />",
"pageGroups":"team",
"priority":"4",
"Snapshot Link":"",
"type":"test"
}发布于 2022-04-08 23:37:20
您可以使用str.split + str.startswith
data = {
"alertOwner": "team",
"assignGroup": "team",
"component": "lnx2",
"Tags": "application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description": "Process ilapd is not running on lnx2, expected state is running,",
"Event Url": "https://app.datadoghq.com/monitors#67856691",
"logicalName": "lnx2",
"Metric Graph": '<img src="" />',
"pageGroups": "team",
"priority": "4",
"Snapshot Link": "",
"type": "test",
}
process = next(
tag.split(":")[-1]
for tag in map(str.strip, data["Tags"].split(","))
if tag.startswith("process:")
)
print(process)指纹:
ilapd或者使用re模块:
import re
r = re.compile(r"process:(.*)")
for t in data["Tags"].split(","):
if (m := r.search(t)) :
print(m.group(1))https://stackoverflow.com/questions/71804073
复制相似问题