1

私は辞書を持っています: (キーは「名前」で、値は他の辞書です)

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': 
                         {'option-domain-name': '"internal.example.org"',
                         'option-domain-name-servers': 'ns1.internal.example.org',
                         'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 
                         'default-lease-time': '600', 'option-routers': '10.5.5.1',
                         'option-broadcast-address': '10.5.5.31'},
              'subnet 10.254.239.32 netmask 255.255.255.224': 
                         {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60',
                          'option-routers': 'rtr-239-32-1.example.org', 
                          'option-broadcast-address': '10.254.239.31'}, 
              'subnet 10.254.239.0 netmask 255.255.255.224': 
                         {'range': '10.254.239.10 10.254.239.20',
                          'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-     
                           2.example.org'}, 
              'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 
                                'fixed-address': 'fantasia.fugue.com'}, 
              'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95',
                                   'server-name': '"toccata.fugue.com"',
                                   'filename': '"vmunix.passacaglia"'}
         }

私がしなければならないのは、すべての「option-」を「option」に変更することだけです

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option domain-name': '"internal.example.org"', 'option domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option routers': '10.5.5.1', 'option broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option routers': 'rtr-239-32-1.example.org', 'option broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}

どうすればいいのですか?

4

1 に答える 1

2
for subdict in dictionary.values():
    for key, val in subdict.items():
        if key.startswith('option-'):
            del subdict[key]
            subdict[' '.join(key.split('-', 1))] = val

したがって、含まれているすべての辞書について、その項目をループし、キーが で始まる場合はoption-、辞書からキーを削除し、ダッシュを削除して新しいキーの下に値を保存します。

デモ:

>>> from pprint import pprint
>>> dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option-domain-name': '"internal.example.org"', 'option-domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option-routers': '10.5.5.1', 'option-broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option-routers': 'rtr-239-32-1.example.org', 'option-broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}
>>> for subdict in dictionary.values():
...     for key, val in subdict.items():
...         if key.startswith('option-'):
...             del subdict[key]
...             subdict[' '.join(key.split('-', 1))] = val
... 
>>> pprint(dictionary)
{'host fantasia': {'fixed-address': 'fantasia.fugue.com',
                   'hardware': 'ethernet 08:00:07:26:c0:a5'},
 'host passacaglia': {'filename': '"vmunix.passacaglia"',
                      'hardware': 'ethernet 0:0:c0:5d:bd:95',
                      'server-name': '"toccata.fugue.com"'},
 'subnet 10.254.239.0 netmask 255.255.255.224': {'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org',
                                                 'range': '10.254.239.10 10.254.239.20'},
 'subnet 10.254.239.32 netmask 255.255.255.224': {'option broadcast-address': '10.254.239.31',
                                                  'option routers': 'rtr-239-32-1.example.org',
                                                  'range': 'dynamic-bootp 10.254.239.40 10.254.239.60'},
 'subnet 10.5.5.0 netmask 255.255.255.224': {'default-lease-time': '600',
                                             'max-lease-time': '7200',
                                             'option broadcast-address': '10.5.5.31',
                                             'option domain-name': '"internal.example.org"',
                                             'option domain-name-servers': 'ns1.internal.example.org',
                                             'option routers': '10.5.5.1',
                                             'range': '10.5.5.26 10.5.5.30'}}
于 2012-06-30T11:01:59.470 に答える