Pretty simple exercise to play and get familiar with the MCFC Bolton_ManCityF24.xml file.
We’re going to parse the file and simply count goals (Event/Type_id=”16″). We will print each goal event plus the team code (30 or 43) and the min:sec of the event.
First conclusion you get when you look at the F24 file is that it is not deep or heavily nested. Everything is organized around “Event”. Every event is associated with shared set of attributes. Quite simple, quite straightforward.
That being said, for more complex operations, it’ll require a tool like high performance in-memory datastore Redis. We’ll see.
So, snippet of code looks like this:
# Little warmup with the file
# We’ll parse the entire file and look for Event/type_id = ’16’ (goal)
# And we’ll output goal + team + timestamp
from lxml import etree
inFile = “./Downloads/Bolton_ManCityF24.xml”
xmlData = etree.parse(inFile) #etree.parse() opens and parses the data
# proceed to loop on all Events
events = xmlData.findall(“//Event”)
for event in events:
eventType = event.attrib[‘type_id’]
team = event.attrib[‘team_id’]
tick = event.attrib[‘min’] + “:” + event.attrib[‘sec’]
if eventType == ’16’:
print “GOAL!” + ” for team ” + team + ” at ” + tick
And output simply is:
GOAL! for team 43 at 25:14
GOAL! for team 43 at 36:59
GOAL! for team 30 at 38:54
GOAL! for team 43 at 46:39
GOAL! for team 30 at 62:6
So team 43 won 3 to 2. Yeah!
For the record, the code was executed on an Ubuntu 11.04 LST equipped box. Current version of Python is 2.7.3
[…] Code snippets from @JBrisson to extract events from the F-24 […]