Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with nested dictionary
#1
myStr = '''Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

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
Reply
#2
I added code tags for you.
It's so much easier to read a dictionary if written like:
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
        }
    }
}
you can use something like the following to traverse a nested dictionary:
def display_dict(thedict):
    for key, value in thedict.items():
        if isinstance(value, dict):
            print(f'{key}:')
            self.display_dict(value)
        else:
            print(f'    {key}: {value}')

display_dict(bond0)
This also corrects errors you had in your definition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 881 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Nested dictionary acting strange Pedroski55 2 2,124 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,602 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,626 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  nested dictionary rkpython 7 3,283 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 3,178 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 1,875 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 2,679 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  How to change value in a nested dictionary? nzcan 2 5,778 Sep-23-2019, 04:09 PM
Last Post: nzcan
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,383 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020