0

I'm trying to parse an xml file using Python which has the following line

<Stat Type="total_pass">37</Stat>

I would like to get the 37 value giving a certain attribute - in this case it would be total_pass.

vftw
  • 1,547
  • 3
  • 22
  • 51
  • What have you tried, and what exactly is the problem with it? – jonrsharpe Jul 30 '19 at 21:22
  • The problem is that I can't get the '37' when the attribute is equal to 'total_pass'. The tag is Stat, the attribute is 'Type' and I want to know the value when the Type is equal to 'total_pass', for example. – vftw Jul 30 '19 at 21:26
  • 1
    That doesn't really answer my question. Give a [mcve]. – jonrsharpe Jul 30 '19 at 21:31

1 Answers1

0

Below

import xml.etree.ElementTree as ET

xml = '<r><Stat Type="total_pass">37</Stat><Stat Type="other">33</Stat></r>'

root = ET.fromstring(xml)
stat_with_total_pass = root.find(".//Stat[@Type='total_pass']")
print(stat_with_total_pass.text)

output

37
balderman
  • 22,927
  • 7
  • 34
  • 52