我开始进行数据分析,我想解析XML文件以便分析数据,这是我的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>
如何访问Stat标签中的preferred_foot?这是我访问这些数据的python代码:
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"]')
答案 0 :(得分:1)
这里是:
这个想法是找到所有Type偏爱的Stat元素
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)
输出
Left
Right