【问题标题】:My dict appears to overwrite and not update with new rows?我的 dict 似乎覆盖而不是更新新行?
【发布时间】:2018-09-24 22:57:47
【问题描述】:

给定以下代码:

https://bpaste.net/show/dd44a1fa01dc

from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("testconfig.txt", syntax="junos")

interfaces = {}
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
    intf_name = intfobj.text.strip()
    interfaces.update({'name': intf_name})
    descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
    interfaces.update({'description': descr})
    mode = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
        all_children=True)
    interfaces.update({'mode': mode})


print (interfaces)

使用以下测试数据:

https://bpaste.net/show/df422a96aaae

interfaces {
    ge-2/0/0 {
        description "site1;;hostname1;ge-16/0/9;;;TRUST;";
        unit 0 {
            family ethernet-switching {
                port-mode trunk;
            }
        }
    }
    ge-2/0/2 {
        description "site2;;hostname2;ge-16/0/8;;;TRUST;";
        unit 0 {
            family ethernet-switching {
                port-mode trunk;
            }
        }
    }

    vstp {
        bpdu-block-on-edge;
        vlan VLAN_0005 {
            interface ge-2/0/0 {
                edge;
            }
        }
        vlan VLAN_0015 {
            interface ge-2/0/0 {
                edge;
            }
            interface ge-2/0/2 {
                edge;                   
            }
        }  
    }
}

我试图理解为什么我的 interfaces = {} 只包含一行:

{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}

我希望它包含来自测试数据的两个接口:

{'name': 'ge-2/0/0', 'description': 'site1;;hostname1;ge-16/0/9;;;TRUST;', 'mode': 'trunk'}
{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}

【问题讨论】:

  • interfaces 是一个字典。它不能包含多个“行”。你需要一个字典或类似的列表。
  • dicts 是映射,而不是序列。每个键都关联(或映射)到单个值。但是,该“单一”值可以是 list 或其他嵌套容器。
  • 你有例子吗?毫无疑问,我对事情感到困惑。 Interfaces 是 dict,在 dict 中每个 ge-x/x/x 应该是一个新的键,其值为 deciption 和 port-mode

标签: python ciscoconfparse


【解决方案1】:

您正在创建一个字典并更新它的值。所以你最终得到一个带有最后一个值的字典。 你可以使用一个字典列表,例如:

from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("testconfig.txt", syntax="junos")

interfaces = []
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
    interface = {}
    interface['name'] = intfobj.text.strip()
    interface['description'] = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
    interface['mode'] = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
        all_children=True)
    interfaces.append(interface)

print (interfaces)

【讨论】:

    【解决方案2】:

    可以试试这样的

    allInterfaces = {}
    interfaces = {}
    intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
    for intfobj in intfs:
        intf_name = intfobj.text.strip()
        interfaces.update({'name': intf_name})
        descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
        interfaces.update({'description': descr})
        mode = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
            all_children=True)
        interfaces.update({'mode': mode})
        allInterfaces = allInterfaces + [interfaces,]
    

    打印(所有接口)

    【讨论】:

    • 糟糕...不够快 :-)
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多