使用 re python 解析 json 值
parse json value with re python
我在 json
中有这个 key:value
"text": "CRITICAL: Alert for device amst-asw1 - Port status Down\nSeverity: critical\nTimestamp: 2021-02-01 10:53:16\nUnique-ID: 307849\nRule: Port status Down Faults:\n #1: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4101; ifDescr = vme; \n #2: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4128; ifDescr = ge-0/0/4; \n #3: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4136; ifDescr = ge-0/0/8; \nAlert sent to:\n",
我只想解析值并从中获取 ifDescr 字符串。
import json
import os
import requests
import re
with open('alerts.json', 'r') as json_file:
data = json.load(json_file)
for a in data['history']:
text = a.get('text')
if re.search(r"ifDescr", text):
print(text)
我明白了
CLEARED: Device amst-asw1 recovered from Port status Down
Severity: critical
Time elapsed: 3d 20h 25m 34s Timestamp: 2021-02-01 10:41:39
Unique-ID: 307845
Rule: Port status Down Faults:
#1: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4101; ifDescr => vme;
#2: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4128; ifDescr => ge-0/0/4;
#3: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4136; ifDescr => ge-0/0/8;
Alert sent to:
我只想要这个
ifDescr = ge-0/0/8
你应该使用正则表达式:
m = re.search('ifDescr\s*=\s*(.*)\s*;', text)
if m:
ifDescr = m.group(1)
正则表达式解释:
ifDescr
: 按字面意思搜索 'ifDescr'
\s*
: 任何数字或 space char
=
: '=' char literally
(.*)
:您要匹配的组(.*
任意数量的任意字符)
\s*;
:任何数字或 space 字符后跟 ';'字面上的字符
我在 json
中有这个 key:value"text": "CRITICAL: Alert for device amst-asw1 - Port status Down\nSeverity: critical\nTimestamp: 2021-02-01 10:53:16\nUnique-ID: 307849\nRule: Port status Down Faults:\n #1: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4101; ifDescr = vme; \n #2: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4128; ifDescr = ge-0/0/4; \n #3: sysObjectID = .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr = Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id = 17; port_id = 4136; ifDescr = ge-0/0/8; \nAlert sent to:\n",
我只想解析值并从中获取 ifDescr 字符串。
import json
import os
import requests
import re
with open('alerts.json', 'r') as json_file:
data = json.load(json_file)
for a in data['history']:
text = a.get('text')
if re.search(r"ifDescr", text):
print(text)
我明白了
CLEARED: Device amst-asw1 recovered from Port status Down
Severity: critical
Time elapsed: 3d 20h 25m 34s Timestamp: 2021-02-01 10:41:39
Unique-ID: 307845
Rule: Port status Down Faults:
#1: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4101; ifDescr => vme;
#2: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4128; ifDescr => ge-0/0/4;
#3: sysObjectID => .1.3.6.1.4.1.2636.1.1.1.2.63; sysDescr => Juniper Networks, Inc. ex4300-32f Ethernet Switch, kernel JUNOS 17.3R3-S4.2, Build date: 2019-04-10 20:40:30 UTC Copyright (c) 1996-2019 Juniper Networks, Inc.; location_id => 17; port_id => 4136; ifDescr => ge-0/0/8;
Alert sent to:
我只想要这个
ifDescr = ge-0/0/8
你应该使用正则表达式:
m = re.search('ifDescr\s*=\s*(.*)\s*;', text)
if m:
ifDescr = m.group(1)
正则表达式解释:
ifDescr
: 按字面意思搜索 'ifDescr'\s*
: 任何数字或 space char=
: '=' char literally(.*)
:您要匹配的组(.*
任意数量的任意字符)\s*;
:任何数字或 space 字符后跟 ';'字面上的字符