【问题标题】:parse json value with re python使用 re python 解析 json 值
【发布时间】:2021-02-01 23:11:17
【问题描述】:

我有这个键:json 中的值

"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

【问题讨论】:

    标签: json python-3.x python-re


    【解决方案1】:

    你应该使用正则表达式:

    m = re.search('ifDescr\s*=\s*(.*)\s*;', text)
    if m:
        ifDescr = m.group(1)
    

    正则表达式解释:

    • ifDescr :按字面意思搜索“ifDescr”
    • \s* : 任意数字或空格字符
    • = : '=' char 字面意思
    • (.*) :你要匹配的组(.* 任意数量的任意字符)
    • \s*; : 任何数字或空格字符后跟 ';'字符字面意思

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多