Python Forum
tuple printing trouble - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: tuple printing trouble (/thread-15991.html)



tuple printing trouble - anna - Feb-09-2019

Hi All,

trying to print interfaces details from router, below is the my code.

from ciscoconfparse import CiscoConfParse
interfaces = {}
cisco_cfg = CiscoConfParse("configuration.txt")
rtr_interfaces = cisco_cfg.find_objects(r"^interface")
for intfaces in rtr_interfaces:
    details = intfaces.children
    interfaces.update({'name': intfaces.text})
    for names in details:
        if 'vlan-type'in names.text:
           vlan = names.text.split(' ')[3]
           if not vlan:
                  interfaces.update({'vlan':' '})
           else:
                  interfaces.update({'vlan': vlan })
        if 'description' in names.text:
            description = names.text.split(' ')[2:]
            if not description:
               interfaces.update({'description': 'n/a'})
            else:
               interfaces.update({'description': description})
        if 'ip address' in names.text:
            ip = names.text.split(' ')[2:]
            if not ip:
               interfaces.update({'ip': 'n/a'})
            else:
               interfaces.update({'ip': ip})
        if 'qos-profile' in names.text:
            policy = names.text.split(' ')[2]
            if not policy:
               #interfaces.update({'policy': 'n/a'})
               if 'user-queue' in names.text:
                  user_queue = names.text.split(' ')[4]
                  if not user_queue:
                     interfaces.update({'policy': 'n/a'})
                  else:
                     interfaces.update({'policy':user_queue})
            else:   # if 'user-queue' in names.text:
               #policy = names.text.split(' ')[2]
               interfaces.update({'policy': policy})
    for details in interfaces.items():
        print("%s"%(details,))
Output:
('name', 'interface Eth-Trunk1.130') ('description', ['EXT_MISUMI', 'INDIA', 'PVT', 'LTD_12405738282']) ('vlan', '130') ('ip', ['address', '111.93.33.201', '255.255.255.248']) ('policy', '30Mb') ('name', 'interface Eth-Trunk1.155') ('description', ['EXT_IREO-12405068279']) ('vlan', '155') ('ip', ['address', '111.93.232.221', '255.255.255.252']) ('policy', 'Internet_8Mb') ('name', 'interface Eth-Trunk1.157') ('description', ['EXT_IREO-12405068279']) ('vlan', '155') ('ip', ['address', '111.93.232.221', '255.255.255.252']) ('policy', 'Internet_8Mb') ('name', 'interface Eth-Trunk1.158') ('description', ['EXT_MATHWORKS', 'INDIA', 'PRIVATE', 'LIMITED_1105725697']) ('vlan', '158') ('ip', ['address', '111.93.206.33', '255.255.255.252']) ('policy', '10Mb')
would like to print like below

name description vlan ip policy
interface Eth-Trunk1.158 EXT_MATHWORKS', 'INDIA', 'PRIVATE', 'LIMITED_1105725697 158 111.93.206.33', '255.255.255.252 10Mb