Python Forum
how to create a nested dict..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to create a nested dict..
#1
its me again, i guess i will never get my head around this language.

i have the following code..

def get_niminfo(self):
        file = '/etc/niminfo'
        niminfo = {}
        niminfo = niminfo['niminfo'] = {}

        try:
            if os.path.exists(file):
                niminfo = dict(
                    (k.strip(), v.strip(' "\n')) for k, v in (
                        line.split(
                            ' ', 1)[1].split(
                                '=', 1) for line in (
                                    (l for l in open(
                                        file, 'r') if l.startswith('export'))) if not re.match(
                                            r'^\s*$', line)))
        except IOError as e:
            module.warnings.append('could not read /etc/niminfo')
            niminfo = {}
        return niminfo
it returns the following (ignore the "ansible_" prefix)...

"ansible_facts": {
        "ansible_NIM_BOSINST_DATA": "/NIM_BOSINST_DATA",
        "ansible_NIM_BOSINST_RECOVER": "/../SPOT/usr/lpp/bos.sysmgt/nim/methods/c_bosinst_env -a hostname=host1",
        "ansible_NIM_BOS_FORMAT": "mksysb",
        "ansible_NIM_BOS_IMAGE": "/NIM_BOS_IMAGE",
        "ansible_NIM_CONFIGURATION": "standalone",
        "ansible_NIM_CUSTOM": "/../SPOT/usr/lpp/bos.sysmgt/nim/methods/c_script -a location=nimserver:/export/nim/scripts/host1.script",
        "ansible_NIM_FIPS_MODE": "0",
        "ansible_NIM_HOSTNAME": "host1",
        "ansible_NIM_HOSTS": "127.0.0.1:loopback:localhost  10.0.0.1:host1  10.0.0.2:nimserver",
        "ansible_NIM_LICENSE_ACCEPT": "yes",
        "ansible_NIM_MASTERID": "00F7089898989",
        "ansible_NIM_MASTER_HOSTNAME": "nimserver",
        "ansible_NIM_MASTER_PORT": "1058",
        "ansible_NIM_MOUNTS": "nimserver:/export/lpp_source/aix72/7200-00-02-1614:/SPOT/usr/sys/inst.images:dir  nimserver:/export/nim/niminstall/bosinstdata/bos_wop_mirror:/NIM_BOSINST_DATA:file  nimserver:/export/mksysb/mksysb_master_aix72_00:/NIM_BOS_IMAGE:file",
        "ansible_NIM_NAME": "host1",
        "ansible_NIM_REGISTRATION_PORT": "1059",
        "ansible_NIM_SHELL": "shell",
        "ansible_RC_CONFIG": "rc.bos_inst",
        "ansible_SPOT": "nimserver:/export/spot/spot_aix72_00/spot_aix72_00/usr", }
but my intention was this

"niminfo": {
        "NIM_BOSINST_DATA": "/NIM_BOSINST_DATA",
        "NIM_BOSINST_RECOVER": "/../SPOT/usr/lpp/bos.sysmgt/nim/methods/c_bosinst_env -a hostname=host1",
        "NIM_BOS_FORMAT": "mksysb",
        "NIM_BOS_IMAGE": "/NIM_BOS_IMAGE",
        "NIM_CONFIGURATION": "standalone",
        "NIM_CUSTOM": "/../SPOT/usr/lpp/bos.sysmgt/nim/methods/c_script -a location=nimserver:/export/nim/scripts/host1.script",
        "NIM_FIPS_MODE": "0",
        "NIM_HOSTNAME": "host1",
        "NIM_HOSTS": "127.0.0.1:loopback:localhost  10.0.0.1:host1  10.0.0.2:nimserver",
        "NIM_LICENSE_ACCEPT": "yes",
        "NIM_MASTERID": "00F7089898989",
        "NIM_MASTER_HOSTNAME": "nimserver",
        "NIM_MASTER_PORT": "1058",
        "NIM_MOUNTS": "nimserver:/export/lpp_source/aix72/7200-00-02-1614:/SPOT/usr/sys/inst.images:dir  nimserver:/export/nim/niminstall/bosinstdata/bos_wop_mirror:/NIM_BOSINST_DATA:file  nimserver:/export/mksysb/mksysb_master_aix72_00:/NIM_BOS_IMAGE:file",
        "NIM_NAME": "host1",
        "NIM_REGISTRATION_PORT": "1059",
        "NIM_SHELL": "shell",
        "RC_CONFIG": "rc.bos_inst",
        "SPOT": "nimserver:/export/spot/spot_aix72_00/spot_aix72_00/usr", }
so how to get the additional dict level in?
Reply


Messages In This Thread
how to create a nested dict.. - by wardancer84 - Nov-22-2018, 02:14 PM
RE: how to create a nested dict.. - by Larz60+ - Nov-22-2018, 02:26 PM
RE: how to create a nested dict.. - by wardancer84 - Nov-22-2018, 03:11 PM
RE: how to create a nested dict.. - by Gribouillis - Nov-22-2018, 03:16 PM
RE: how to create a nested dict.. - by wardancer84 - Nov-22-2018, 03:20 PM
RE: how to create a nested dict.. - by Larz60+ - Nov-23-2018, 04:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating nested dict list keys tbaror 2 1,295 Feb-09-2022, 09:37 AM
Last Post: tbaror
  changing key names in nested dict wardancer84 6 2,164 Sep-10-2021, 08:13 AM
Last Post: wardancer84
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,092 Jun-26-2021, 11:12 AM
Last Post: Larz60+
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,908 Jan-22-2021, 05:43 AM
Last Post: buran
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,451 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Sort a dict in dict cherry_cherry 4 76,304 Apr-08-2020, 12:25 PM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,221 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Easy way to sort a nested dict Alfalfa 3 5,671 Dec-07-2018, 04:12 PM
Last Post: Alfalfa
  Better way to create nested dictionary with defaultdict() x2mlh 8 21,689 Nov-30-2017, 08:10 PM
Last Post: buran
  sorting nested dict according to values merlem 6 17,644 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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