How do I configure multiple dictionary keys in a yaml file in Python?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How do I configure multiple dictionary keys in a yaml file in Python?



I am trying to use a yaml file to store dictionary keys so I can parse various json payloads.



I have the following yaml file loaded into a variable called 'cfg':


my_table: metadata



When I run:


for i in json['resources']:
print(i[cfg['my_table']])



I can see all values from my json's ['metadata'] keys just fine.



However, I would like to see all values from the ['metadata']['guid'] keys that exist under ['resources']. So when I change my yaml file to:


my_table: metadata, guid



It doesn't work. I've also tried these variations:


my_table: ['metadata', 'guid'] #tried ''.join("['0']".format(a) for a in cfg['my_table']) which gives me what I want, but when I shove this inside i, I get a key error
my_table: ['metadata']['guid'] #yaml file doesn't parse properly



Any ideas?





Well if you trying to map the yaml properties then the value's structure should be the one which python can understand. try my_table: [metadata, guid]
– mad_
Aug 10 at 15:52


my_table: [metadata, guid]





Can we see an except of your json? Is guid a key of the metadata object or are they seperate keys in the resources object?
– FHTMitchell
Aug 10 at 15:52



guid


metadata


resources





You need i['metadata']['guid'], which is two separate indexing operations on i and i['metadata'], not a single one that you can capture in one expression. That is, there's no x for which i[x] is i['metadata']['guid'].
– chepner
Aug 10 at 15:52



i['metadata']['guid']


i


i['metadata']


x


i[x] is i['metadata']['guid']




1 Answer
1



So you want to store dictionary keys in a yaml file?



Just do


# keys.yaml
my_table: [metadata, guid]



And then in your python file


keys = yaml.safe_load('keys.yaml')['my_table']

for obj in json['resources']: # don't call it json, that's a package name
print(obj[keys[0]][keys[1]])



For a variable number of keys (not only two) do it like so:


def recursive_getitem(obj, keys):
if len(keys) == 0:
return obj
return recursive_getitem(obj[keys[0]], keys[1:])



and then do


for obj in json['resources']:
print(recursive_getitem(obj, keys))



To answer your second question in the comments, you need to use a nested list


# keys.yaml
my_table:
- [metadata, guid]
- [entity, name]



.


# py file
keys = yaml.safe_load('keys.yaml')['my_table']

for obj in json['resources']: # don't call it json, that's a package name
print(obj[keys[0][0]][keys[0][1]])
print(obj[keys[1][0]][keys[1][1]])





Since the OP is using PyYAML, using yaml.load() is unsafe as per the PyYAML documentation. It also completely unnecessary here, yaml.safe_load() will do.
– Anthon
Aug 10 at 16:08


yaml.load()


yaml.safe_load()





Fair enough, i'm not very experienced in yaml so I'll edit.
– FHTMitchell
Aug 10 at 16:10





Yes, that works for me! I can now place: [metadata, guid] in my yaml file. By any chance, how would I get ['metadata']['guid'], then get another pair like ['entity']['name']. If I place [[metadata, guid], [entity,name]] in my yaml file, it doesn't load properly. That said, you answered my original question so I will accept the answer.
– nollie11
Aug 10 at 20:49





@nollie11 see my edit
– FHTMitchell
Aug 10 at 21:13





Thank you @FHTMitchell - is there a recursive function for a variable number of not just items in each list but # of lists?
– nollie11
Aug 10 at 21:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard