0

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?

1 Answers1

0

Not sure if I really got the problem but here we go:

First of all I would recommend you to avoid using variable names like j and i; it's easier to understand what the code is doing if variable names are descriptive of what they hold.

Indeed you are using any() the wrong way. any() receives an iterable an returns True if at least one element in that iterable is True and False otherwise.

One small change in what you already have to make it work could be replacing the second for loop for the following:

for j in inis:
    if j['iniDescTipo'] == "Proposta de Lei":
        i = j['iniEventos']['pt_gov_ar_objectos_iniciativas_EventosOut']
        if any(map(lambda elem: elem["fase"] == "Decreto (Publicação)", i):
            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

This way, you are transforming your array i in a list of booleans which are True if the element has a fase == "Decreto (Publicacao)" and False otherwise; which is the input any() needs.

Regarding the try: except: part of the code, I'm pretty sure it could be better but, without too much knowledge of what these objects contain it's hard to recommend a better solution.

Hope this helps!

  • Hi, thank you for your answer. It didn't really solve my problem, because i can't relate the ````any()```` function with the output. I should've explained my intentions better. But it gave me an idea. Can you tell me how can i introduce some variation of this ````any()```` function as a replacement for the 1 and 0 outputs? My problem would be solved if I could get "True" and "False" in the ````pl.update````, conditional on the existence of that key's value. – João Jerónimo Apr 28 '20 at 19:59
  • Ok, so the idea would be to put a `"1"` if the result of `any()` was `True` and `"0"` otherwise? If that's the case, then you could switch the `try:` for the `if` conditional, and the `except` for the `else`. That way, in every case where `any() == True` you will set `"1"` and in any other case you will set `"0"`. Sorry if I'm still not getting the objective. – Rodrigo De Rosa Apr 28 '20 at 20:11