Extract and print specific xml values using Python

Asked

Viewed 1,143 times

2

Hello :)

I am trying to capture data from an xml to use in a search using python, but my algorithm is only returned the data from the last tags, example: The xml is found in http://legis.senado.gov.br/dadosabertos/senador/lista/atual?uf=sp and within it, there are three tags containing the data of parliamentarians, the tags have the name "Parliamentary", when running the code, the same only returns the data of the last parliamentarian and not the data of the 3, follows code used:

import requests
import xmltodict
import dicttoxml
from xml.etree import ElementTree as elements

URL = "http://legis.senado.gov.br/dadosabertos/senador/lista/atual?uf=sp"

dados = requests.get(url=URL)
dadosx = xmltodict.parse(dados.content)
dadosxml = dicttoxml.dicttoxml(dadosx)

root = elements.fromstring(dadosxml)
levels = root.findall('.//IdentificacaoParlamentar')
for level in levels:
name = level.find('NomeParlamentar').text
code = level.find('CodigoParlamentar').text
print name, code

This code is only returning me:

Marta Suplicy 5000

Could someone tell me where I’m going wrong?

Thanks for your attention :)

2 answers

2


The blocks of code Python "work" according to your indentation, your code is only returning 1 name as it goes through all for and only "escreve" the last item in the name variable, your code should be:

import requests
import xmltodict
import dicttoxml
from xml.etree import ElementTree as elements

URL = "http://legis.senado.gov.br/dadosabertos/senador/lista/atual?uf=sp"

dados = requests.get(url=URL)
dadosx = xmltodict.parse(dados.content)
dadosxml = dicttoxml.dicttoxml(dadosx)

root = elements.fromstring(dadosxml)
levels = root.findall('.//IdentificacaoParlamentar')
for level in levels:
  name = level.find('NomeParlamentar').text
  code = level.find('CodigoParlamentar').text
  print(name)
  print(code)

Exit:

Airton Sandoval 5140

José Serra 90

Marta Suplicy 5000

On that website is talked a little about the functioning of indentation

  • 2

    Thank you very much Barbetta, I’ll read the reference you gave me ;)

1

Missing print indentation for iterated values to print

for level in levels:
    name = level.find('NomeParlamentar').text
    code = level.find('CodigoParlamentar').text
    print(name, code)

OUTPUT:

Airton Sandoval 5140
José Serra 90
Marta Suplicy 5000
  • Thank you very much Willian, that’s right the/ o/ o/ o/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.