I have a dictionary that in turn contains dictionaries that look something like this:
variable={'query': {'query_string': {'query': 'E.keyword: {sustituir_0}'}}}
The idea is to create a function that obtains the keys of each dictionary and returns them in an array, to later change {substitute_0} for the desired word.
The function used is recursive and it is this
def funcion_recursiva(diccionario,resultados=None):
if resultados is None:
resultados = []
if (type(diccionario)==str):
return resultados
elif (type(diccionario)==dict):
clave=diccionario.keys()[0]
resultados.append(clave)
return funcion_recursiva(diccionario[clave],resultados)
We apply the function to our variable
resul = funcion_recursiva(variable)
and we try to apply a for loop to advance in the dictionary
z=''
for i in range(len(resul)):
z=z+"["+resul[i]+"]"
if(i==range(len(resul))-1):
variable[z]=variable[z].replace('{sustituir_0}',"Madrid")
This is where my error comes from and my question when I do variable [z] it gives me an error and if I try to concatenate too.
If I have all the dictionary keys in dictionaries in a list, how can one access the dictionary fields using the for loop?