Click here to Skip to main content
16,019,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this type of code xml:

<pre><?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2001-2012 Opta Sportsdata Ltd. All rights reserved. -->

<!-- PRODUCTION HEADER
     produced on:        valde-jobq-a03.nexus.opta.net
     production time:    20120911T092033,103Z
     production module:  Opta::Feed::XML::Soccer::F24
-->
<Games timestamp="2012-09-11T10:20:32">
  <Game id="360481" away_team_id="43" away_team_name="Manchester City" competition_id="8" competition_name="English Barclays Premier League" game_date="2011-08-21T16:00:00" home_team_id="30" home_team_name="Bolton Wanderers" matchday="2" period_1_start="2011-08-21T16:00:38" period_2_start="2011-08-21T17:03:47" season_id="2011" season_name="Season 2011/2012">
    <Event id="301038339" event_id="1" type_id="34" period_id="16" min="0" sec="0" team_id="43" outcome="1" x="0.0" y="0.0" timestamp="2011-08-21T15:23:06.696" last_modified="2011-08-21T15:54:56">
      <Q id="2028397186" qualifier_id="130" value="4" />
      <Q id="1518776786" qualifier_id="227" value="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" />
      <Q id="997025056" qualifier_id="59" value="25, 2, 13, 18, 4, 6, 42, 7, 10, 16, 21, 5, 11, 15, 20, 22, 32, 45" />
      <Q id="955425655" qualifier_id="194" value="17476" />
      <Q id="996147927" qualifier_id="131" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0" />
      <Q id="1940069841" qualifier_id="44" value="1, 2, 2, 3, 2, 2, 3, 3, 4, 4, 3, 5, 5, 5, 5, 5, 5, 5" />
      <Q id="1529687618" qualifier_id="30" value="15749, 20492, 42593, 1632, 17476, 7551, 14664, 15157, 42544, 37572, 20664, 20658, 19959, 65807, 56827, 17336, 20312, 42493" />
    </Event>
    <Event id="1475524684" event_id="1" type_id="34" period_id="16" min="0" sec="0" team_id="30" outcome="1" x="0.0" y="0.0" timestamp="2011-08-21T15:39:39.166" last_modified="2011-08-21T16:06:40">
      <Q id="1993329296" qualifier_id="59" value="22, 2, 4, 6, 5, 12, 7, 19, 17, 14, 10, 1, 3, 16, 20, 21, 31, 38" />
      <Q id="783602879" qualifier_id="131" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0" />
      <Q id="1981808255" qualifier_id="30" value="1344, 28183, 2004, 27696, 19419, 1587, 18428, 14668, 9765, 3630, 10089, 45175, 82263, 19930, 1615, 15188, 19958, 105088" />
      <Q id="1521261840" qualifier_id="194" value="3630" />
      <Q id="459356083" qualifier_id="227" value="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" />
      <Q id="2003349974" qualifier_id="130" value="2" />
      <Q id="1582676412" qualifier_id="44" value="1, 2, 2, 3, 2, 2, 3, 3, 4, 4, 3, 5, 5, 5, 5, 5, 5, 5" />
    </Event>
    <Event id="2036897618" event_id="2" type_id="32" period_id="1" min="0" sec="0" team_id="30" outcome="1" x="0.0" y="0.0" timestamp="2011-08-21T16:00:38.967" last_modified="2011-08-21T16:00:39">
      <Q id="530297025" qualifier_id="127" value="Left to Right" />
    </Event>
    <Event id="336246484" event_id="2" type_id="32" period_id="1" min="0" sec="0" team_id="43" outcome="1" x="0.0" y="0.0" timestamp="2011-08-21T16:00:39.132" last_modified="2011-08-21T16:00:39">
      <Q id="1227488973" qualifier_id="127" value="Right to Left" />
    </Event>


What I have tried:

I created an empty list for each column to populate the dataframe:

  team = []
time = []
minute = []
second = []
origenX = []
origenY = []
destinateX = []
destinateY = []
result = []


but now I have to iterate each event and filter those that correspond to passes (attribute type_id="1"), adding to each list of the previous step the corresponding attributes of each event.

so for this I do

root = ET.parse('OptaF24.xml').getroot()

for nodo in root.iterfind("[@type_id='1']"):
    for elemento in nodo.iter():
        print(elemento.text)


maybe I have to change its format with a df= DataFrame but I'm not sure

but it doesn't print anything, I don't know what code error I have since it's not very long.

finally I would use a data.head() to see the result. Maybe I need to change the data to csv o something like that

I need to "create" columns only with the type_id=1
Posted
Updated 27-Oct-22 20:43pm
v2
Comments
Richard MacCutchan 27-Oct-22 9:45am    
There are no items in the XML above that contain type_id='1'.
David Vazquez Bande 27-Oct-22 9:47am    
well it is only a piece of the code, but in the complete code it have type_id=1

1 solution

I have created a test XML file and can find the nodes with the following code:
Python
for nodo in root.iterfind(".//*[@type_id='1']"):
    print(nodo.tag)

See xml.etree.ElementTree — The ElementTree XML API — Python 3.11.0 documentation[^] for full details and syntax for search terms.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900