我正在尝试使用Python解析某个XML文件。我在此站点上尝试了各种解决方案,但似乎没有一个能产生我想要的结果。
我具有以下XML结构:
Team A General Info
- Team A Player 1 Info
- Team A Player 2 Info
- ...
Team B General Info
- Team B Player 1 Info
- Team B Player 2 Info
- ...
以下使用BeautifulSoup的代码接近了,但没有产生我想要的东西。
此代码产生以下输出: [“ A队”,“ B队”] ['John1','Jane1','John2','Jane2']
但是,我要列出的每个团队的球员如下: ['A队','John1','Jane1'] [“ B团队”,“ John2”,“ Jane2”]
from bs4 import BeautifulSoup
import csv
data= """
<xml>
<record>
<team_general_info>
<team_name>Team A</team_name>
<team_color>Red</team_color>
</team_general_info>
<player_info>
<player_name>John1</player_name>
<player_dob>01/01/01</player_dob>
<player_id>11111</player_id>
</player_info>
<player_info>
<player_name>Jane1</player_name>
<player_dob>01/01/02</player_dob>
<player_id>22222</player_id>
/player_info>
</record>
<record>
<team_general_info>
<team_name>Team B</team_name>
<team_color>Green</team_color>
</team_general_info>
<player_info>
<player_name>John2</player_name>
<player_dob>02/02/01</player_dob>
<player_id>33333</player_id>
</player_info>
<player_info>
<player_name>Jane2</player_name>
<player_dob>02/02/02</player_dob>
<player_id>44444</player_id>
/player_info>
</record>
</xml>
"""
soup = BeautifulSoup(data, "html.parser")
required = soup.find_all("team_name")
Team_General=[]
for i in required:
Team_General.append(i.get_text())
print (Team_General)
required = soup.find_all("player_name")
Player_Info=[]
for i in required:
Player_Info.append(i.get_text())
print (Player_Info)