I have this data called RemainingBlocks and RemainingInfoBlocks :
bloquesRestantes = ['Articulos', 'Proyectos']
bloquesInfoRestante = {
"Articulos":[
{
"pais":"Peru",
"indice":"Artículos Publicados de Divulgación Local",
"fecha_publicacion":"2005-02-04",
"mapeo":[
"Pais",
"Indice",
"Fecha Publicacion"
]
},
{
"pais":"Turquia",
"indice":"Artículos",
"fecha_publicacion":"1995-02-04",
"mapeo":[
"Pais",
"Indice",
"Fecha Publicacion"
]
}
],
"Proyectos":[
{
"fecha_inicio":"2005-02-01",
"codigo_proyecto":"",
"descripcion":"Asadsadsadsadsa sadsadsadsa",
"mapeo":[
"Fecha Inicio",
"Codigo Proyecto",
"Descripcion",
]
},
{
"fecha_inicio":"2008-08-08",
"codigo_proyecto":"008",
"descripcion":"Descripcion test",
"mapeo":[
"Fecha Inicio",
"Codigo Proyecto",
"Descripcion",
]
}
]
}
I use that information and through the zip() function I map the information with the data element mapeo
in bloquesInfoRestante
the following way:
listaData = dict()
for i in bloquesRestantes:
for bloqueInformacion in bloquesInfoRestante[i]:
resultados = dict(zip(bloqueInformacion['mapeo'], bloqueInformacion.values()))
listaData[i] = [{k:v for k,v in resultados.items()}]
print(listaData)
So far so good, now in the line where it is listaData[i]
what I try is that for each dictionary key bloquesInfoRestante
that are 'Articles', 'Projects' . to be able to obtain the information of the list of each one.
But the way I'm doing it, I'm only getting the last element of each key in bloquesInfoRestante
the following way:
listaData = {
"Articulos":[
{
"Pais":"Turquia",
"Indice":"Artículos",
"Fecha Publicacion":"1995-02-04"
}
],
"Proyectos":[
{
"Fecha Inicio":"2008-08-08",
"Codigo Proyecto":"008",
"Descripcion":"Descripcion test",
}
],
}
When my intention is to get the data like this:
listaData = {
"Articulos":[
{
"Pais":"Peru",
"Indice":"Artículos Publicados de Divulgación Local",
"Fecha Publicacion":"2005-02-04"
},
{
"Pais":"Turquia",
"Indice":"Artículos",
"Fecha Publicacion":"1995-02-04"
}
],
"Proyectos":[
{
"Fecha Inicio":"2005-02-01",
"Codigo Proyecto":"",
"Descripcion":"Asadsadsadsadsa sadsadsadsa",
},
{
"Fecha Inicio":"2008-08-08",
"Codigo Proyecto":"008",
"Descripcion":"Descripcion test",
}
],
}
My question is how can I get the information in this way. I hope you can help me and thanks in advance.
Since we want to remove the key
"mapeo"
inside each contained element from the dictionary"Articulos"
and"Proyectos"
(bloquesRestantes
), the dictionaries inPython
have a methodpop
.For this, it is necessary to iterate the elements contained in both
"Articulos"
and"Proyectos"
(bloquesRestantes
), taking into account that, when iterating the elements in a dictionary, they do not always maintain the same order.In this case, as the structure shown always has the key
"mapeo"
, an index can be createdenumerate
for this purpose.Additionally, it is required to replace the keys
"codigo_proyecto"
a""Codigo Proyecto""
for them use the methodreplace
and title.