我有一个(巨大的).xml文件,该文件或多或少是以下代码段的一种变体,我设法在其中加载了Vim。我一直想做的是找到子标记market
的名字“正确分数”的地方,然后我需要在下面的合同中列出所有信息,或者通过下面的合同进行迭代的方法
有人可以帮我吗?我已经阅读了所有基本示例,但是我的文件与在线示例不同,它是凌晨2点...
import xml.etree.ElementTree as ET
#__________________________________
tree = ET.parse('get_this.xml')
root = tree.getroot()
for child in root:
print "Match Names", child.attrib['name']
print
rows = root.findall('.//market')
for child in rows:
if child.attrib['slug'] == 'correct-score':
print child.attrib
print child.attrib['id']
#when I have found the id and correct score market, how do I work with the data below it?
<?xml version='1.0' encoding='utf-8'?>
<odds timestamp="2018-09-28T00:50:15Z">
<event date="2018-09-28" id="960747" name="Babelsberg vs. ZFC Meuselwitz"
parent="/sport/football/germany-regionalliga-2018-2019"
parent_name="Germany Regionalliga" parent_slug="/sport/football/germany-regionalliga-2018-2019"
state="upcoming" time="17:00:00"
type="Football match"
url="/sport/football/germany-regionalliga/2018/09/28/sv-babelsberg-03-vs-zfc-meuselwitz">
<market id="7777534" slug="winner" traded_volume="0" winners="1">
<contract id="26245385" name="Draw" slug="draw">
<bids>
<price backers_stake="364.54" decimal="4.9" liability="1421.56" percent="20.41"/>
</bids>
<offers>
<price backers_stake="449.21" decimal="3.35" liability="191.15" percent="29.85"/>
</offers>
</contract>
<contract id="26245384" name="ZFC Meuselwitz" slug="away">
<bids>
<price backers_stake="360.35" decimal="5.3" liability="1549.28" percent="18.87"/>
</bids>
<offers>
<price backers_stake="507.56" decimal="3.55" liability="199.05" percent="28.17"/>
</offers>
</contract>
<contract id="26245383" name="Babelsberg" slug="home">
<bids>
<price backers_stake="361.58" decimal="2.20" liability="433.98" percent="45.45"/>
</bids>
<offers>
<price backers_stake="146.07" decimal="1.80" liability="182.62" percent="55.56"/>
</offers>
</contract>
</market>
<market id="7777540" slug="correct-score" traded_volume="4" winners="1">
<contract id="26245430" name="Any other draw" slug="any-other-draw">
<bids>
<price backers_stake="0.00" decimal="10000" liability="13.24" percent="0.01"/>
</bids>
<offers>
<price backers_stake="13.68" decimal="44" liability="0.32" percent="2.27"/>
</offers>
</contract>
<contract id="26245429" name="Any other away win" slug="any-other-away-win">
<bids>
<price backers_stake="0.00" decimal="100" liability="0.24" percent="1.0"/>
</bids>
<offers>
<price backers_stake="0.15" decimal="17.0" liability="0.01" percent="5.88"/>
</offers>
</contract>
<contract id="26245428" name="Any other home win" slug="any-other-home-win">
<bids>
<price backers_stake="0.01" decimal="28" liability="0.18" percent="3.57"/>
</bids>
<offers>
<price backers_stake="0.22" decimal="11.0" liability="0.02" percent="9.09"/>
</offers>
</contract>
<contract id="26245427" name="3 - 3" slug="3-3">
<bids>
<price backers_stake="0.00" decimal="10000" liability="23.56" percent="0.01"/>
</bids>
<offers>
<price backers_stake="5.46" decimal="30" liability="0.19" percent="3.33"/>
</offers>
</contract>
<contract id="26245426" name="3 - 2" slug="3-2">
<bids>
<price backers_stake="0.00" decimal="10000" liability="28.01" percent="0.01"/>
</bids>
<offers>
<price backers_stake="3.15" decimal="20.0" liability="0.17" percent="5.0"/>
</offers>
</contract>
</market>
</event>
</odds>
答案 0 :(得分:1)
如果您想在市场标签下以条形名称作为正确的分数来获得完整的信息,请尝试使用以下代码。
import xml.etree.ElementTree as ET
tree = ET.parse('./sample.xml')
root = tree.getroot()
for child in root.findall('.//market'):
if child.attrib['slug'] == 'correct-score':
for ele in child.iter():
print(ele.tag,ele.attrib)
我将您的xml文件保存在sample.xml中,希望对您有帮助