I have yaml file and I want to get specific set of key-value pairs. How I can do that?
DEV:
A: "A_DEV_config.xml"
B: "B_DEV_config.xml"
C: "C_DEV_config.xml"
PROD:
D: "D_PROD_config.xml"
E: "E_PROD_config.xml"
F: "E_PROD_config.xml"
OTHERS:
G: "G_config.xml"
H: "H_config.xml"
K: "K_config.xml"
I want to get something like this:
if I check DEV == true:
output list of keys -> A,B,C,G,H,K
if I check PROD == true:
output list of keys -> D,E,F,G,H,K
As you may have noticed, I need to get OTHERS
in both cases.
I tried to use code:
stream = open(yml_file, 'r')
data = yaml.safe_load(stream)
print(data.get("DEV").keys())
But I don't know how to concatenated it together
To concatenate the keys (DEV or PROD) with the keys from the OTHERS section, you need to define a function to fetch the keys based on the environment and concatenate with OTHERS either way.
Below I have written a function:
import yaml
with open(yml_file, 'r') as stream:
data = yaml.safe_load(stream)
def get_keys(environment):
env_keys = list(data.get(environment, {}).keys())
others_keys = list(data.get("OTHERS", {}).keys())
return env_keys + others_keys
if DEV == True:
print(get_keys("DEV"))
elif PROD == True:
print(get_keys("PROD"))
Hope this helps!