python - 难以从 smarkets 解析 .xml 文件

标签 python xml python-2.7

我有一个(巨大的).xml 文件,它或多或少是我设法用 Vim 加载的以下代码片段的变体。我一直在尝试做的是找到子标签 market 的 slug 名称为“正确分数”,然后我需要列出下面合约中的所有信息,或者迭代的方法以下契约(Contract)。

有人可以帮我吗?我已经完成了所有基本示例,但我的文件与在线示例不同,而且现在是凌晨 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>

最佳答案

如果您想要市场标签下的完整信息,并以 slug 名称作为正确的分数,请尝试使用以下代码。

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中,希望对你有帮助

关于python - 难以从 smarkets 解析 .xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52939596/

相关文章:

python - 对矩阵中的元素与另一个矩阵中的相应元素进行平均(在Python中)

python - 如何用Python在文本文件中搜索多个关键字

android - 如何设置背景图像的透明度 Android xml 文件

python - pylab 找不到其模块的引用

python-2.7 - 根据类型或类别列出 Microsoft Azure 计算中的 VM 大小

python - 拆分功能 - 避免最后的空白空间

python - 通过股票行情中的破折号或点从雅虎财经下载数据

未检测到 C# XElement.Load 方法?

Python ElementTree 解析后不会更新新文件

Python 3,range().append() 返回错误 : 'range' object has no attribute 'append'