Xpath lxml 获取重复子元素的id
Xpath lxml obtain id of repeated subelement
例如,这是我的缩写来源。
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
</meeting>
我的代码为每个种族元素循环了正确的次数,但是它没有将 id 的检索移动到下一个元素。我如何相对于循环移动树?
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'])
您需要在循环内使用循环变量。替换:
for races in root.race.iter():
print(root.race.attrib['id'])
与:
for race in root.race.iter():
print(race.attrib['id'])
仅仅因为您没有得到正在迭代的当前对象元素,它应该是:
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')
例如,这是我的缩写来源。
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
<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 000. First 750, second 750, third 50, fourth 50, fifth 00, sixth 00, seventh 00, eighth 00, ninth 00, tenth 00">
</meeting>
我的代码为每个种族元素循环了正确的次数,但是它没有将 id 的检索移动到下一个元素。我如何相对于循环移动树?
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'])
您需要在循环内使用循环变量。替换:
for races in root.race.iter():
print(root.race.attrib['id'])
与:
for race in root.race.iter():
print(race.attrib['id'])
仅仅因为您没有得到正在迭代的当前对象元素,它应该是:
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')