I am trying to export some data from the following json: http://app.parlamento.pt/webutils/docs/doc.txt?path=6148523063446f764c324679626d56304c3239775a57356b595852684c3052685a47397a51574a6c636e52766379394a626d6c6a6157463061585a686379394a53556b6c4d6a424d5a57647063327868644856795953394a626d6c6a6157463061585a6863306c4a53563971633239754c6e523464413d3d&fich=IniciativasIII_json.txt&Inline=true.
Speciffically, whenever iniDescTipo == "Proposta de Lei"
, I am looking for iniNr
, dataInicioleg
and dataFimleg
. Also, I'm looking for dataFase
when fase == "Anúncio"
, and, whenever there's a dictionary within iniEvntos
with fase == "Decreto (Publicação)"
, I want an output of 1 in every row of the output file, and 0 otherwise. The intended output should look like this:
Title Number InicioLeg FimLeg DataAnuncio Aprovado
a 12 1991 1995 1992 1
b 32 1991 1995 1991 1
c 14 1991 1995 1991 0
My problem is the binary variable. As of now, my code is as follows:
r = requests.get('http://app.parlamento.pt/webutils/docs/doc.txt?path=6148523063446f764c324679626d56304c3239775a57356b595852684c3052685a47397a51574a6c636e52766379394a626d6c6a6157463061585a686379394a53556b6c4d6a424d5a57647063327868644856795953394a626d6c6a6157463061585a6863306c4a53563971633239754c6e523464413d3d&fich=IniciativasIII_json.txt&Inline=true')
r.raise_for_status()
data = r.json()
inis = data['ArrayOfPt_gov_ar_objectos_iniciativas_DetalhePesquisaIniciativasOut']['pt_gov_ar_objectos_iniciativas_DetalhePesquisaIniciativasOut']
for j in inis:
if j['iniDescTipo'] == "Proposta de Lei":
for i in j['iniEventos']['pt_gov_ar_objectos_iniciativas_EventosOut']:
if any("Decreto (Publicação)"):
try:
pl.update({j['iniTitulo']: [j['iniTitulo'], j['iniNr'], j['dataInicioleg'], j['dataFimleg'], j['iniEventos']['pt_gov_ar_objectos_iniciativas_EventosOut'][0]['dataFase'], "1"]})
except KeyError:
pl.update({j['iniTitulo']: [j['iniTitulo'], j['iniNr'], j['dataInicioleg'], j['dataFimleg'], j['iniEventos']['pt_gov_ar_objectos_iniciativas_EventosOut']['dataFase'], "0"]})
else:
pass
I don't believe I've used the if any
properly, hence the struggle with this last vavriable. I've tried some tips from Check if value already exists within list of dictionaries?, Loop through all nested dictionary values? and Check if value already exists within list of dictionaries?, but with no success. Can anyone point me to my mistake and, perhaps, suggest me a way to get all of the 5 variables?