I'm transforming fields from an XML document so I can load into a normal relational DB. I've transformed the XML document to a bunch of nested dictionaries. Some of the values I wish to extract are in nested dictionaries, so I need to flatten it first.
Easy enough, but I'd like to create a mapping that lets me specify upfront what to extract.
Example
input_dict = {
'authors': [{'name': u'Google, Inc.'}],
'islink': False,
}
mapping = ['islink',<???>]
Desired output
In: tuple(input_dict[key] for key in mapping)
Out: (False, 'Google, Inc.')
This obviously doesn't work:
In: [input_dict[key] for key in ['islink',['authors'][0]['name']]]
Out: TypeError: string indices must be integers, not str