Question

I am trying to get an xml file into a dataframe so I can experiment with creating some graphs.

The file I have is xml 20140508GOSF0.xml

This is a sample of it.

<meeting id="35483" barriertrial="0" venue="Gosford" date="2014-05-08T00:00:00" gearchanges="-1" stewardsreport="-1" gearlist="-1" racebook="0" postracestewards="0" meetingtype="TAB" rail="True" weather="Fine      " trackcondition="Dead      " nomsdeadline="2014-05-02T11:00:00" weightsdeadline="2014-05-05T16:00:00" acceptdeadline="2014-05-06T09:00:00" jockeydeadline="2014-05-06T12:00:00">
  <club abbrevname="Gosford Race Club" code="49" associationclass="2" website="http://" />
  <race id="185273" number="1" nomnumber="7" division="0" name="GOSFORD ROTARY MAIDEN HANDICAP" mediumname="MDN" shortname="MDN" stage="Acceptances" distance="1600" minweight="55" raisedweight="0" class="MDN       " age="~         " grade="0" weightcondition="HCP       " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="22000" first="12250" second="4250" third="2100" fourth="1000" fifth="525" time="2014-05-08T12:30:00" bonustype="BX02      " nomsfee="0" acceptfee="0" trackcondition="          " timingmethod="          " fastesttime="          " sectionaltime="          " formavailable="0" racebookprize="Of $22000. First $12250, second $4250, third $2100, fourth $1000, fifth $525, sixth $375, seventh $375, eighth $375, ninth $375, tenth $375">
    <condition line="1">Of $22000. First $12250, second $4250, third $2100, fourth $1000, fifth $525, sixth $375, seventh $375, eighth $375, ninth $375, tenth $375</condition>
    <condition line="2">Starter Subsidy: $200 for non-prize earning runners.</condition>
    <condition line="3">Maiden, Handicap, For No age restriction, No sex restriction</condition>
    <condition line="4">BOBS&amp;BOBS Extra  Bonus available: $5,000</condition>
    <condition line="5">Apprentices can claim. Field Limit: 14 + 4 EM</condition>
    <nomination number="1" saddlecloth="1" horse="Olympic Academy" id="182542" idnumber="" regnumber="" blinkers="1" trainernumber="233" trainersurname="Walter" trainerfirstname="Guy" trainertrack="Warwick Farm" rsbtrainername="Guy Walter" jockeynumber="40133" jockeysurname="Shinn" jockeyfirstname="Blake" barrier="5" weight="59" rating="0" description="B G 3 Magic Albert x Laguna Lass (Orpen(USA))" colours="Lime And Black Check, White Sleeves, Lime Cap" owners="P F Cruickshank, J Nikolovski, Mrs P H Jacobs, J Hasson, W R Jacobs, Mrs S Riethmuller, R E Harwood, D Jacobs &amp; M P Cruickshank " dob="2010-09-25T00:00:00" age="4" sex="G" career="3-0-1-1 $5525.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="1-0-0-0 $525.00" heavytrack="2-0-1-1 $5000.00" slowtrack="0-0-0-0" deadtrack="0-0-0-0" fasttrack="0-0-0-0" firstup="1-0-0-1 $2100.00" secondup="1-0-1-0 $2900.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="59" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="E" />
    <nomination number="2" saddlecloth="2" horse="Carnival Knight" id="176969" idnumber="" regnumber="" blinkers="1" trainernumber="696" trainersurname="Frazer" trainerfirstname="Garry" trainertrack="Hawkesbury" rsbtrainername="Garry Frazer" jockeynumber="623" jockeysurname="Brewer" jockeyfirstname="Robbie" barrier="8" weight="57.5" rating="0" description="B G 3 Oratorio(IRE) x Boleyn (Marscay)" colours="White And Green Diamonds, Black Sleeves, Green Cap" owners="Mrs K Pinczuk, M Pinczuk &amp; Miss L Pinczuk " dob="2010-08-24T00:00:00" age="4" sex="G" career="2-0-0-0 $550.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-0-0 $550.00" heavytrack="0-0-0-0" slowtrack="0-0-0-0" deadtrack="0-0-0-0" fasttrack="0-0-0-0" firstup="1-0-0-0 $275.00" secondup="1-0-0-0 $275.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="57.5" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="E" />
    <nomination number="3" saddlecloth="3" horse="Golden Rule" id="178525" idnumber="" regnumber="" blinkers="1" trainernumber="750" trainersurname="Cowell" trainerfirstname="Fred" trainertrack="Gosford" rsbtrainername="Fred Cowell" jockeynumber="76420" jockeysurname="Lisnyy" jockeyfirstname="Serg" barrier="2" weight="57" rating="0" description="CH G 3 Valixir(IRE) x Pleasure Ground (Arena)" colours="Black, Grey Diamonds, Yellows Sleeves and Cap" owners="G J Andison, S Andison, Miss S R Andison, Miss C M Andison, Mrs I E Cowell, Miss O James &amp; R G Williams " dob="2010-09-27T00:00:00" age="4" sex="G" career="5-0-0-2 $5950.00" thistrack="2-0-0-1 $2475.00" thisdistance="0-0-0-0" goodtrack="2-0-0-1 $2475.00" heavytrack="2-0-0-0 $1375.00" slowtrack="0-0-0-0" deadtrack="1-0-0-1 $2100.00" fasttrack="0-0-0-0" firstup="2-0-0-1 $2475.00" secondup="1-0-0-0 $1000.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="57" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="E" />
    <nomination number="4" saddlecloth="4" horse="Deb's Home" id="184038" idnumber="" regnumber="" blinkers="1" trainernumber="47495" trainersurname="Newing" trainerfirstname="Tony" trainertrack="Gosford" rsbtrainername="Tony Newing" jockeynumber="73283" jockeysurname="Kissick" jockeyfirstname="John" barrier="4" weight="56.5" rating="0" description="BR M 4 Home On the Grange x Tisharma (Armidale)" colours="Blue, Pink Crossed Sashes, Pink Armbands, Quartered Cap, Blue Pom Pom" owners="S G S Lester &amp; Mrs D M Lester " dob="2009-10-06T00:00:00" age="5" sex="M" career="1-0-0-0 $525.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="0-0-0-0" heavytrack="0-0-0-0" slowtrack="0-0-0-0" deadtrack="1-0-0-0 $525.00" fasttrack="0-0-0-0" firstup="1-0-0-0 $525.00" secondup="0-0-0-0" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="56.5" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="K" />
    <nomination number="5" saddlecloth="5" horse="Speed of Life" id="178810" idnumber="" regnumber="" blinkers="1" trainernumber="78011" trainersurname="Poletti" trainerfirstname="Turner" trainertrack="Warwick Farm" rsbtrainername="Amanda Turner &amp; Mauro Poletti" jockeynumber="1037" jockeysurname="Ford" jockeyfirstname="Jay" barrier="3" weight="56.5" rating="0" description="BR G 3 Authorized(IRE) x Nostalgic (Quest for Fame(GB))" colours="Purple, Pale Blue Sash" owners="Ms A M Turner &amp; M G Poletti " dob="2010-09-03T00:00:00" age="4" sex="G" career="3-0-0-0 $1175.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="2-0-0-0 $650.00" heavytrack="0-0-0-0" slowtrack="0-0-0-0" deadtrack="1-0-0-0 $525.00" fasttrack="0-0-0-0" firstup="2-0-0-0 $900.00" secondup="1-0-0-0 $275.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="56.5" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="E" />
    <nomination number="6" saddlecloth="6" horse="Hickory Lad" id="174582" idnumber="" regnumber="" blinkers="1" trainernumber="1808" trainersurname="Thomas" trainerfirstname="Stan" trainertrack="Wyong" rsbtrainername="Stan Thomas" jockeynumber="57545" jockeysurname="Spriggs" jockeyfirstname="Blake" barrier="1" weight="55" rating="0" description="CH G 3 Strada x Hickory Snip (Snippets)" colours="Red, Black Band, Armbands And Cap" owners="S Thomas " dob="2010-09-30T00:00:00" age="4" sex="G" career="9-0-0-1 $4950.00" thistrack="2-0-0-0 $750.00" thisdistance="4-0-0-0 $1550.00" goodtrack="4-0-0-0 $1550.00" heavytrack="2-0-0-0 $650.00" slowtrack="1-0-0-0 $375.00" deadtrack="2-0-0-1 $2375.00" fasttrack="0-0-0-0" firstup="2-0-0-0 $650.00" secondup="2-0-0-0 $650.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="55" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="E" />
    <nomination number="7" saddlecloth="7" horse="Speedy Wally" id="173253" idnumber="" regnumber="" blinkers="0" trainernumber="626" trainersurname="Conners" trainerfirstname="Marc" trainertrack="Warwick Farm" rsbtrainername="Marc Conners" jockeynumber="1878" jockeysurname="Taylor" jockeyfirstname="Jessica" barrier="6" weight="55" rating="0" description="BR G 3 Shinzig x Mota (At Talaq(USA))" colours="White, Grey Spots, White And Grey Halved Sleeves, Pink Cap" owners="A S K Ko " dob="2010-09-10T00:00:00" age="4" sex="G" career="9-0-1-2 $10100.00" thistrack="1-0-0-0 $375.00" thisdistance="3-0-1-1 $6725.00" goodtrack="5-0-0-1 $3375.00" heavytrack="0-0-0-0" slowtrack="2-0-0-1 $2100.00" deadtrack="2-0-1-0 $4625.00" fasttrack="0-0-0-0" firstup="2-0-0-0 $750.00" secondup="2-0-0-1 $2100.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="55" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
    <nomination number="8" saddlecloth="8" horse="Whitney's Girl" id="166285" idnumber="" regnumber="" blinkers="0" trainernumber="52355" trainersurname="Williams" trainerfirstname="Noel" trainertrack="Warwick Farm" rsbtrainername="Noel Williams" jockeynumber="888" jockeysurname="Spriggs" jockeyfirstname="Dale" barrier="7" weight="55" rating="0" description="BR M 4 General Nediym x Life is Beautiful (Quest for Fame(GB))" colours="Emerald Green, Red Straight Yoke, White Southern Cross, Red Halved Sleeves, Quartered Cap" owners="Mrs D A Williams, W C Williams, R T Williams, G D Williams &amp; R Williams " dob="2009-10-07T00:00:00" age="5" sex="M" career="12-0-1-1 $8525.00" thistrack="0-0-0-0" thisdistance="2-0-1-0 $3175.00" goodtrack="4-0-1-0 $4475.00" heavytrack="3-0-0-0 $650.00" slowtrack="0-0-0-0" deadtrack="5-0-0-1 $3400.00" fasttrack="0-0-0-0" firstup="3-0-0-0 $1275.00" secondup="3-0-0-0 $650.00" mindistancewin="0" maxdistancewin="0" finished="0" weightvariation="0" variedweight="55" decimalmargin="0.00" penalty="0" pricestarting="" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="K" />
  </race>
  <race id="185274" number="2"

this is what I have done so far.

from lxml import objectify

path = '/home/sayth/Scripts/va_benefits/20140508GOSF0.xml'

parsed = objectify.parse(open(path))

root = parsed.getroot()

data = []

for elt in root.meeting:
    el_data = {}
    for child in elt.getchildren():
        el_data[child.tag] = child.pyval
    data.append(el_data)

I am fairly new at using xml but meeting is the root element to me. But it returns this error

AttributeError: no such child: meeting

Why does it not return as the root and how can I get it too?

Edit: Charles has noted that I already have root and don't need to refer to an attribute whether its meeting or not.

However when I take the code and insert print statements to see any output I cannot get any output from root or child elements.

This returns nothing.

from lxml import objectify

path = '/home/sayth/Scripts/va_benefits/20140508GOSF0.xml'

parsed = objectify.parse(open(path))

root = parsed.getroot()

data = []
print root

for elt in root:
    print elt
Was it helpful?

Solution

root.meeting doesn't return anything because your root variable itself already is the meeting element. You don't need to do anything else to get it -- you already have it.


The following session is demonstrative:

>>> import lxml.objectify
>>> lxml.objectify.parse(open('test.xml'))
<lxml.etree._ElementTree object at 0x10a0037a0>
>>> doc = lxml.objectify.parse(open('test.xml'))
>>> root = doc.getroot()
>>> root
<Element meeting at 0x10a003a70>
>>> print root.getchildren()
[u'', <Element race at 0x10a004710>]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top