myStr = '''Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
I began doing some coding as shown below, but still not getting there :
Output:Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer2+3 (2)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
802.3ad info
LACP rate: slow
Min links: 0
Aggregator selection policy (ad_select): stable
Active Aggregator Info:
Aggregator ID: 2
Number of ports: 2
Actor Key: 11
Partner Key: 705
Partner Mac Address: 02:1c:73:9c:3c:fe
Slave Interface: p1p1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 9c:dc:71:45:eb:80
Aggregator ID: 2
Slave queue ID: 0
Slave Interface: p4p1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 9c:dc:71:4d:80:20
Aggregator ID: 2
Slave queue ID: 0'''
I have the above text outputs and I want to create a nested dictionary as follows:bond0 : { 'MII Status:' : 'up', 'Aggregator ID:' : '2', 'Slave Interfaces' : { 'p1p1' : { 'MII Status' : 'up', 'Permanent HW addr' : '9c:dc:71:45:eb:80', 'MII Status' : up }, 'p4p1' : { ''MII Status' : 'up', 'Permanent HW addr' : '9c:dc:71:4d:80:20', 'MII Status' : up }, },
I began doing some coding as shown below, but still not getting there :
from __future__ import print_function class BndClass(dict): def __init__(self, Bnd=None): self['Name'] = Bnd self.uPdateInfo() super(BndClass, self).__init__() def uPdateInfo(self): for line in myStr.splitlines(): match = re.search(r'^Cur.*?:\s+(.*?)$', line) if match: self['act_int'] = match.group(1) match = re.search(r'^\s*?Aggregator ID:\s+(\d)$', line) if match: self['agid'] = match.group(1) match = re.search(r'^Slave\sInterface:\s(.*?)$', line) if match: self.setdefault('slvs', []).append(match.group(1)) if __name__ == '__main__': Y = BndClass('bond0') Y {'Name': 'bond0', 'agid': '2', 'slvs': ['p1p1', 'p4p1']}please help