python - 从 cli 字符串输出中识别键和值的算法

标签 python algorithm language-agnostic string-comparison

我有一个命令 show interface transciever 其输出可能会有所不同。 我已经为不同的场景收集了这个命令的一些示例输出。

Ethernet1/2
    transceiver is present
    type is 1000base-T
    name is CISCO-METHODE
    part number is SP7041-E-R
    revision is --
    serial number is MTC19350BBK
    nominal bitrate is 1300 MBit/sec
    Link length supported for copper is 100 m
    cisco id is 3
    cisco extended id number is 4

Ethernet1/3
    transceiver is present
    type is 1000base-T
    name is CISCO-METHODE
    part number is SP7041-E
    revision is E
    serial number is MTC150303LW
    nominal bitrate is 1300 MBit/sec
    Link length supported for copper is 100 m
    cisco id is 3
    cisco extended id number is 4

Ethernet1/4
    transceiver is present
    type is 1000base-T
    name is CISCO-METHODE
    part number is SP7041-E
    revision is E
    serial number is MTC1731072M
    nominal bitrate is 1300 MBit/sec
    Link length supported for copper is 100 m
    cisco id is 3
    cisco extended id number is 4

Ethernet1/5
    transceiver is not present

Ethernet1/6
    transceiver is not present

Ethernet1/7
    transceiver is present
    type is 10Gbase-SR
    name is CISCO-AVAGO
    part number is SFBR-709SMZ-CS1
    revision is G4.1
    serial number is AVD1718A4WN
    nominal bitrate is 10300 MBit/sec
    Link length supported for 50/125um OM2 fiber is 82 m
    Link length supported for 62.5/125um fiber is 26 m
    Link length supported for 50/125um OM3 fiber is 300 m
    cisco id is 3
    cisco extended id number is 4
    cisco part number is 10-2415-03
    cisco product id is SFP-10G-SR
    cisco vendor id is V03

^^ 一些示例。 我想比较这些不同的样本并确定潜在的键和值。

喜欢:

  • 存在收发器
  • 收发器不存在

transceiver is 可以是键,present/not present 可以是值。 同样,我想解析这些输出样本以识别其他键和值。是否有标准算法 可以引导我朝着正确的方向实现这一目标。

最佳答案

我不知道标准算法,但对于这个例子,看起来你应该搜索 ' is ' 的实例并将键定义为前面的文本和值作为后续文本。

这是一个例子:

# Sample data
string = '''Ethernet1/2
    transceiver is present
    type is 1000base-T
    name is CISCO-METHODE
    part number is SP7041-E-R
    revision is --
    serial number is MTC19350BBK
    nominal bitrate is 1300 MBit/sec
    Link length supported for copper is 100 m
    cisco id is 3
    cisco extended id number is 4'''

sub = string.split('\n')

d = {}
d[sub[0]] = [x.strip() for x in sub[1:]]

e = {}
for key in d.keys():
    e[key] = {}
    for item in d[key]:
        new = item.split(' is ')
        e[key][new[0]] = new[1]
print(e)
{'Ethernet1/2': 
    {'transceiver': 'present', 
     'type': '1000base-T', 
     'name': 'CISCO-METHODE', 
     'part number': 'SP7041-E-R', 
     'revision': '--', 
     'serial number': 'MTC19350BBK', 
     'nominal bitrate': '1300 MBit/sec', 
     'Link length supported for copper': '100 m', 
     'cisco id': '3', 
     'cisco extended id number': '4'}}

如果存在不止一个级别的实例,您可以通过额外的解析来识别这些实例,例如搜索 ' for ' 的实例并重复该过程。

关于python - 从 cli 字符串输出中识别键和值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433635/

相关文章:

algorithm - 查找并连接子图

language-agnostic - 代码可以持续多长时间?

Python 扭曲了 Web 服务器缓存并执行过时的代码

python - 在Python中获取传入UDP数据包的TTL

python - 如何通过 XPath 查找具有两个可能类名的元素?

python - Jinja、Flask 和 WTForms : how to pass parameters in field?

python - 生成一个列表 a(n) 不是 prime + a(k), k < n 的形式

c - 在 C 中搜索唯一元素组合的算法(结果字符串中的元素位置无关紧要)

swift - 这个矩阵相邻单元格计数的解决方案有什么问题? ( swift )

algorithm - 连续分数项的良好压缩方案?