GOOGLE ADS

sábado, 16 de abril de 2022

¿Cómo puedo imprimir TODOS mis datos json anidados al final de cada línea "padre" en python?

import json
with open('json.json') as f:
data = json.load(f)
for event in data['input']:
edition = event.get('edition', '')
name = event['name']
start_date = event['startDate']
end_date = event.get('endDate', '')
for location in event['location']:
city= location.get('city', '')
state= location.get('state', '')
country= location.get('country', '')


print(edition,name,start_date,end_date,city,state,country)
---
{
"input": [
{
"edition": "4th",
"name": "JBCN Conference",
"startDate": "2018-06-11",
"endDate": "2018-06-13",
"location": [
{
"city": "Barcelona",
"country": "Spain"
}
]
},
{
"edition": "3rd",
"name": "DevTernity",
"startDate": "2018-11-30",
"endDate": "2018-12-01",
"location": [
{
"city": "Riga",
"country": "Latvia"
}
]
},
{
"edition": "1st",
"name": "I T.A.K.E Unconference",
"startDate": "2016-05-19",
"endDate": "2016-05-20",
"location": [
{
"city": "Bucharest",
"country": "Romania"
},
{
"city": "Maramures",
"country": "Romania"
}
]
},
{
"edition": "2nd",
"name": "Product Owner Rule Book",
"startDate": "2016-04-11",
"endDate": "2016-04-13",
"location": [
{
"city": "Paris",
"country": "France"
},
{
"city": "Madrid",
"country": "Spain"
}
]
},
{
"name": "Upfront Summit",
"startDate": "2018-02-01",
"location": [
{
"city": "Los Angeles",
"state": "California",
"country": "United States"
}
]
},
{
"name": "IBM Think",
"startDate": "2018-03-19",
"location": [
{
"state": "Nevada",
"country": "United States"
}
]
}
]
}

Solo obtengo el último elemento anidado para ciudad, estado, país, PERO quiero imprimir todas las ciudades, estados y países de cada evento en la misma línea.

Salida de corriente:


  • IV Congreso JBCN 2018-06-11 2018-06-13 Barcelona España

  • Tercera DevTernity 2018-11-30 2018-12-01 Riga Letonia

  • 1º TOMO Desconferencia 2016-05-19 2016-05-20 Maramures Rumanía

  • 2º Product Owner Rule Book 2016-04-11 2016-04-13 Madrid España

  • Upfront Summit 2018-02-01 Los Ángeles California Estados Unidos

  • IBM Think 2018-03-19 Nevada Estados Unidos


Salida deseada:


  • IV Congreso JBCN · 2018-06-11 / 2018-06-13 · Barcelona, ​​España

  • Tercera DevTernity · 2018-11-30 / 2018-12-01 · Riga, Letonia

  • 1º TOMO Desconferencia · 2016-05-19 / 2016-05-20 · Bucarest | Maramures, Rumanía

  • Libro de reglas del 2º Product Owner · 2016-04-11 / 2016-04-13 · París, Francia | Madrid, España

  • Cumbre Upfront · 2018-02-01 · Los Ángeles, California. Estados Unidos

  • IBM Think · 2018-03-19 · Nevada, Estados Unidos



Solución del problema

Porque en tu ubicación hay más de una ubicación. Es por eso que la ciudad, el estado y el país quedan en último lugar. Mira, te pones city = firstCityy luego te pones city = secondCity. Aquí su ubicación for loopestablece el último valor para las variables. Y lo imprimes fuera del bucle de ubicación. simplemente imprímalo en el bucle de ubicación o haga algo similar a mi código.

for event in data['input']:
edition = event.get('edition', '')
name = event['name']
start_date = event['startDate']
end_date = event.get('endDate', '')
loc = ''
for location in event['location']:
city = location.get('city', '')
state = location.get('state', '')
country = location.get('country', '')
if city:
loc += city
if state:
loc += ' | ' + state
if country:
loc += ' | ' + country
if location!= event['location'][-1]:
loc += ', '
print(edition, name, start_date, end_date, loc)

y aquí está la salida:

4th JBCN Conference 2018-06-11 2018-06-13 Barcelona | Spain
3rd DevTernity 2018-11-30 2018-12-01 Riga | Latvia
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Bucharest | Romania, Maramures | Romania
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Paris | France, Madrid | Spain
Upfront Summit 2018-02-01 Los Angeles | California | United States
IBM Think 2018-03-19 | Nevada | United States

Aquí, si imprime dentro del bucle de ubicación:

for event in data['input']:
edition = event.get('edition', '')
name = event['name']
start_date = event['startDate']
end_date = event.get('endDate', '')
for location in event['location']:
city = location.get('city', '')
state = location.get('state', '')
country = location.get('country', '')
print(edition, name, start_date, end_date, city, state, country)

su salida sería:

4th JBCN Conference 2018-06-11 2018-06-13 Barcelona Spain
3rd DevTernity 2018-11-30 2018-12-01 Riga Latvia
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Bucharest Romania
1st I T.A.K.E Unconference 2016-05-19 2016-05-20 Maramures Romania
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Paris France
2nd Product Owner Rule Book 2016-04-11 2016-04-13 Madrid Spain
Upfront Summit 2018-02-01 Los Angeles California United States
IBM Think 2018-03-19 Nevada United States

si no desea repetir las ubicaciones impresas. Solo haz algo similar a mi primer ejemplo.

No hay comentarios.:

Publicar un comentario

Flutter: error de rango al acceder a la respuesta JSON

Estoy accediendo a una respuesta JSON con la siguiente estructura. { "fullName": "FirstName LastName", "listings...