如何使用TextFSM提取一组跨越多行到单个列表值的重复数据?

时间:2019-01-07 08:57:07

标签: python-textfsm

我最近开始使用TextFSM从半格式文本中提取数据。提取的数据需要映射到简单类型(例如,字符串),复杂类型(例如,相关项的组,例如struct)或复杂类型的集合/列表之一。

i / p数据示例:(注意:输入内容不一定总是JSON)

{
    "cookie":"1539123566/5c904721-d477-17d4-9310-860a18d646cc",
    "response":"yes",
    "classId":"equipmentPsu",
    "outConfigs":[
     {
         "id":"1",
         "serial":"LIT193518YH",
         "thermal":"ok",
         "faultInst":[
         {
             "ack":"yes",
             "cause":"power-problem",
             "code":"F0883",
             "created":"2018-09-20T01:22:18",
             "descr":"***",
             "affectedDN":"sys/chassis-1/psu-1",
             "highestSeverity":"critical",
             "severity":"critical",
             "tags":"server",
         },
         {
              "ack":"yes",
              "cause":"equipment-inoperable",
              "code":"F0374",
              "created":"2018-09-20T01:22:17",
              "descr":"***",
              "affectedDN":"sys/chassis-1/psu-1",
              "severity":"major",
              "tags":"server",
         }
         ]
     },
     {
         "id":"2",
         "serial":"LIT193518Z0",
         "thermal":"ok",
         "faultInst":[
         {
              "ack":"yes",
              "cause":"equipment-inoperable",
              "code":"F0374",
              "created":"2018-09-20T01:22:17",
              "descr":"***",
              "severity":"major",
              "tags":"server",
         }
         ]
    }
    ]
}

我有兴趣提取serialfaultInst,它们是复杂类型Fault的列表

Fault {
    cause,
    code,
    severity
}

使用以下模板,我将“原因”,“代码”和“严重性”作为单独的列表:

Value Required,Filldown SerialNumber(\S+)
Value List FaultCause(\S+)
Value List FaultCode(\S+)
Value List FaultSeverity(\S+)

Start
  ^"serial":->Continue.Record
  ^"serial":"${SerialNumber}"
  ^"faultInst":\[->Fault

Fault
  ^"cause":"${FaultCause}"
  ^"code":"${FaultCode}"
  ^"severity":"${FaultSeverity}"->Fault
  ^\]->Start

是否有一种方法可以提取每个序列号下的faultInst,其中faultInst中的每个项目都是Fault类型的?

请指导。

0 个答案:

没有答案