0

I'm beginnning with data analysis, I want to parse an XML file in order to analyze data, Here is a portion of my XML file :

<?xml version="1.0" encoding="UTF-8"?>
<SoccerFeed>
  <SoccerDocument>
    <Team>
      <Founded>1919</Founded>
      <Name>Angers</Name>
      <Player uID="p40511">
        <Name>Denis Petric</Name>
        <Position>Goalkeeper</Position>
        <Stat Type="first_name">Denis</Stat>
        <Stat Type="last_name">Petric</Stat>
        <Stat Type="preferred_foot">Left</Stat>
      </Player>
      <Player uID="p119744">
        <Name>Mathieu Michel</Name>
        <Position>Goalkeeper</Position>
        <Stat Type="first_name">Mathieu</Stat>
        <Stat Type="preferred_foot">Right</Stat>
      </Player>
    </Team>
  </SoccerDocument>
</SoccerFeed>

How can I access to the preferred_foot in Stat tag ? Here is my python code to access these data :

dom = ElementTree.parse(full_file)
teams = dom.findall('SoccerDocument/Team')
for t in teams:
    team_name = t.find('Name').text
    founded = t.find('Founded').text
    players = t.findall('Player')
    for pl in players:
        player_name = pl.find('Name').text
        player_position = pl.find('Position').text
        preferred_foot = pl.find('Stat[@Type="preferred_foot"]')
2

1 Answer 1

1

Here it is:

The idea is to find all Stat elements where Type is prefered_foot

import xml.etree.ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?>
<SoccerFeed>
  <SoccerDocument>
    <Team>
      <Founded>1919</Founded>
      <Name>Angers</Name>
      <Player uID="p40511">
        <Name>Denis Petric</Name>
        <Position>Goalkeeper</Position>
        <Stat Type="first_name">Denis</Stat>
        <Stat Type="last_name">Petric</Stat>
        <Stat Type="preferred_foot">Left</Stat>
      </Player>
      <Player uID="p119744">
        <Name>Mathieu Michel</Name>
        <Position>Goalkeeper</Position>
        <Stat Type="first_name">Mathieu</Stat>
        <Stat Type="preferred_foot">Right</Stat>
      </Player>
    </Team>
  </SoccerDocument>
</SoccerFeed>'''

root = ET.fromstring(xml)
prefered_foots = root.findall(".//Stat[@Type='preferred_foot']")
for foot in prefered_foots:
    print(foot.text)

Output

Left
Right
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.