2015-11-07 30 views
0

这是我的例子。Xpath lxml获取重复子元素的编号

<meeting> 
    <race id="204091" number="1" nomnumber="4" division="0" name="FAIRFIELD RSL CLUB HANDICAP" mediumname="BM90" shortname="BM90" stage="Results" distance="1200" minweight="54" raisedweight="0" class="BM90  " age="3U  " grade="0" weightcondition="HCP  " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T12:35:00" bonustype="BOB7  " nomsfee="0" acceptfee="0" trackcondition="Good 3 " timingmethod="Electronic" fastesttime="1-09.89 " sectionaltime="600/33.95 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000"> 
    <race id="204092" number="2" nomnumber="5" division="0" name="DOOLEYS HANDICAP" mediumname="BM80" shortname="BM80" stage="Results" distance="1500" minweight="54" raisedweight="0" class="BM80  " age="3U  " grade="0" weightcondition="HCP  " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T13:15:00" bonustype="BOB7  " nomsfee="0" acceptfee="0" trackcondition="Good 3 " timingmethod="Electronic" fastesttime="1-30.01 " sectionaltime="600/34.27 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000"> 
    <race id="204093" number="3" nomnumber="2" division="0" name="CANTERBURY HURLSTONE PARK RSL CLUB SPRING PREVIEW" mediumname="SPRINGPREV" shortname="SPRINGPREV" stage="Results" distance="1400" minweight="54" raisedweight="0" class="~   " age="3U  " grade="0" weightcondition="HCP  " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" totalprize="85000" first="48750" second="16750" third="8350" fourth="4150" fifth="2000" time="2015-08-15T13:50:00" bonustype="BOB7  " nomsfee="0" acceptfee="0" trackcondition="Good 3 " timingmethod="Electronic" fastesttime="1-24.61 " sectionaltime="600/33.06 " formavailable="0" racebookprize="Of $85000. First $48750, second $16750, third $8350, fourth $4150, fifth $2000, sixth $1000, seventh $1000, eighth $1000, ninth $1000, tenth $1000"> 
</meeting> 

我的代码循环每个种族元素正确的次数但它不标识的检索移动到下一个元素。我如何将树移动到循环中?

from lxml import objectify, etree 
from builtins import len 

with open('20150815RHIL0.xml', 'r') as f: 
    xml = f.read() 
    root = objectify.fromstring(xml) 

    #=========================================================================== 
    # print(root.tag) 
    # print(root.attrib['date'], root.attrib['weather']) 
    # print(root.race.attrib['id']) 
    #=========================================================================== 


    for races in root.race.iter(): 
     print(root.race.attrib['id']) 

回答

1

只是因为你没有得到正在迭代的当前对象的元素,它应该是:

root = objectify.fromstring(xml) 

for race in root.meeting.iter('race'): 
    print(race.get('id')) 

此外,根据您的XML片段,root.race不是一个有效的路径,它应该是root.meeting.race。既然你想抓住所有的种族元素,因此:root.meeting.iter('race')

1

您需要在循环中使用循环变量。替换:

for races in root.race.iter(): 
    print(root.race.attrib['id']) 

有:

for race in root.race.iter(): 
    print(race.attrib['id'])