首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的dict似乎覆盖而不是用新行进行更新?

我的dict似乎覆盖而不是用新行进行更新?
EN

Stack Overflow用户
提问于 2018-09-24 22:57:47
回答 2查看 90关注 0票数 1

考虑到以下代码:

https://bpaste.net/show/dd44a1fa01dc

代码语言:javascript
复制
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

代码语言:javascript
复制
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;                   
            }
        }  
    }
}

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

代码语言:javascript
复制
{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}

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

代码语言:javascript
复制
{'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'}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-24 23:14:44

您正在创建一个dict并更新它的值。所以你最终得到的是最后一个值。您可以使用这样的dicts列表:

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2018-09-24 23:19:43

可以试试这样的东西吗

代码语言:javascript
复制
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,]

打印(allInterfaces)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52488411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档