当前位置: 动力学知识库 > 问答 > 编程问答 >

Python Parsing XML into new seperate rows

问题描述:

here is my xml file

 <goal>

<value>

<comment>n</comment>

<stats>

<goals>1</goals>

<shoton>1</shoton>

</stats>

<event_incident_typefk>406</event_incident_typefk>

<elapsed>22</elapsed>

<player2>38807</player2>

<subtype>header</subtype>

<player1>37799</player1>

<sortorder>5</sortorder>

<team>10261</team>

<id>378998</id><n>295</n>

<type>goal</type>

<goal_type>n</goal_type>

</value>

<value>

<comment>n</comment>

<stats>

<goals>1</goals>

<shoton>1</shoton>

</stats>

<event_incident_typefk>393</event_incident_typefk>

<elapsed>24</elapsed>

<player2>24154</player2>

<subtype>shot</subtype>

<player1>24148</player1>

<sortorder>4</sortorder>

<team>10260</team>

<id>379019</id><n>298</n>

<type>goal</type>

<goal_type>n</goal_type>

</value>

</goal>

here is my function for parsing the xml

def extract_goal_type(data):

#print(data)

ret_data = []

if data :

root = ET.fromstring(data)

tree = ET.ElementTree(root)

root = tree.getroot()

for c in root.getchildren():

ret_data.append(c.findtext('subtype'))

return ret_data

the result which l am currently recieving is

 [header, shot]

however l would like it to look like this

header

shot

with each new result been in its own new row on the table ... Can you please help me to get to the desired output .... thank you so much !!!

分享给朋友:
您可能感兴趣的文章:
随机阅读: