简体   繁体   中英

Editing python dictionary keys in nested structures

I have the following structure to edit, and I am not sure how to do it. Eg I need to rename 'Email' into 'emails' and have only email address in the value.

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': [{'Address': 'someaddress@gmail.com', 'Label': 'personal'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}
}

This is what I would like

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'emails': ['someaddress@gmail.com'],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}

I have had a look at similar questions and some documentation, but still can't figure it out.

if d is your dict then Try this:

d['response']['emails'] = [i['Address'] for i in d['response']['Email']]
del d['response']['Email']

Or you can do it using pop in just one line:

d['response']['emails'] = [i['Address'] for i in d['response'].pop('Email')]

This will give you:

 {'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John',
  'emails': ['someaddress@gmail.com']}}

I wouldn't rename a key, but you could just create 'emails' and then populate it:

test_dict = {
    'input_text': 'some text',
    'lang': 'eng',
    'response': {
        'Certification': [{
            'CertificationName': 'some_name'
        }],
        'Email': [{
            'Address': 'someaddress@gmail.com',
            'Label': 'personal'
        }],
        'ExecutiveSummary': ' text',
        'FamilyName': 'Smith',
        'FormattedName': 'John',
        'GivenName': 'John'
    }
}
test_dict['response']['emails'] = [
    email['Address']
    for email in test_dict['response']['Email']
]

Edit: And as @h4z3 had mentioned, you can delete using del or if you need to get the value whilst deleting it pop

# Using del
del test_dict['response']['Email']

# Using pop
test_dict['response'].pop('Email')

If you know that there will be only 'Email' :

from ast import literal_eval

od = {'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': [{'Address': 'someaddress@gmail.com', 'Label': 'personal'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}}

od['response']['Email'] = [od['response']['Email'][0]['Address']]
print(literal_eval(str(od).replace('Email:', 'email:')))

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': ['someaddress@gmail.com'],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}}

You can make new key emails and after that delete the old one:

>>> my_dict = {
...     'input_text': 'some text',
...     'lang': 'eng',
...     'response': {
...         'Certification': [
...             {
...                 'CertificationName': 'some_name'
...             }
...         ],
...         'Email': [
...             {
...                 'Address': 'someaddress@gmail.com', 
...                 'Label': 'personal'
...             }
...         ],
...         'ExecutiveSummary': ' text',
...         'FamilyName': 'Smith',
...         'FormattedName': 'John',
...         'GivenName': 'John'
...     }
... }
>>> my_dict['response']['emails'] = [d['Address'] for d in my_dict['response']['Email']]
>>> my_dict['response'].pop('Email', None)
>>> print(my_dict)
    {
...     'input_text': 'some text',
...     'lang': 'eng',
...     'response': {
...         'Certification': [
...             {
...                 'CertificationName': 'some_name'
...             }
...         ],
...         'ExecutiveSummary': ' text',
...         'FamilyName': 'Smith',
...         'FormattedName': 'John',
...         'GivenName': 'John',
...         'emails': ['someaddress@gmail.com']
...     }
... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM