如果语句不能在ElementTree解析中工作,为什么会这样?

阿比舍克

我正在尝试xml使用如下ElementTree形式解析文件

<Game>
  <Event timestamp="2016-08-14T14:23:33.634" id="1713385925" 
         version="1471181110290" last_modified="2016-08-14T14:25:11" y="11.0" 
         x="89.7" outcome="0" team_id="148" player_id="51327" sec="8" min="23" 
         period_id="1" type_id="4" event_id="205">

    <Q id="733814222" qualifier_id="265"/>
    <Q id="481660420" qualifier_id="286"/>
    <Q id="813378778" qualifier_id="152"/>
    <Q id="570443899" qualifier_id="56" value="Right"/>
    <Q id="420312891" qualifier_id="233" value="248"/>
    <Q id="1186861264" qualifier_id="13"/>
  </Event>

  <Event timestamp="2016-08-14T14:23:33.634" id="1635888622" 
         version="1471181110289" last_modified="2016-08-14T14:25:11" y="89.0" 
         x="10.3" outcome="1" team_id="143" player_id="169007" sec="8" min="23" 
         period_id="1" type_id="4" event_id="248">

    <Q id="1871787686" qualifier_id="56" value="Back"/>
    <Q id="176295814" qualifier_id="13"/>
    <Q id="69346842" qualifier_id="233" value="205"/>
    <Q id="1588029344" qualifier_id="265"/>
    <Q id="559785299" qualifier_id="285"/>
    <Q id="380723313" qualifier_id="152"/>
  </Event>
</Game>

我正在使用的代码很简单,并且可以按预期工作。但是,当我尝试向if condition代码中添加时,一切都会改变

import xml.etree.ElementTree as ET

root = ET.parse(r'C:\Users\ADMIN\Desktop\Abhishek\PSG - Copy\Sample.xml').getroot()

Games = root.getchildren()
for Game in Games:
    Events = Game.getchildren()
    for Event in Events:
        type_id = Event.attrib["type_id"]
        team_id = Event.attrib["team_id"]
        Qualifiers = Event.getchildren()
        for Qualifier in Qualifiers:
            id_ = Qualifier.attrib['id']
            if id_ == 142:
                print ("val")

这是它产生的错误:

Warning (from warnings module):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\PSGPossessionSequences.py", line 9
    Games = root.getchildren()
DeprecationWarning: This method will be removed in future versions.  Use 'list(elem)' or iteration over elem instead.

Warning (from warnings module):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\PSGPossessionSequences.py", line 11
    Events = Game.getchildren()
DeprecationWarning: This method will be removed in future versions.  Use 'list(elem)' or iteration over elem instead.

Warning (from warnings module):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37\PSGPossessionSequences.py", line 15
    Qualifiers = Event.getchildren()
DeprecationWarning: This method will be removed in future versions.  Use 'list(elem)' or iteration over elem instead.

我试着删除,if statement并且效果很好。但是,我确实需要设置一个条件来调用所有id_具有确定值的。我尝试使用"142"以及,142但问题仍然存在。为什么会这样呢?

马丁·彼得斯(Martijn Pieters)

您看到的错误不是错误,而是警告您可以不使用它们而忽略它们,或者使它们静默或修复代码.get_children()您可以直接在每个XML元素上进行迭代:

root = ET.parse(r'C:\Users\ADMIN\Desktop\Abhishek\PSG - Copy\Sample.xml').getroot()

for Game in root:
    for Event in Game:
        # ...
        for Qualifier in Event:

if测试不起作用,因为XML属性是字符串,文本而不是整数值。测试字符串:

if id_ == "142":
    print("val")

您可能要使用XPath查询,而不是遍历所有内容。但是,Python随附的基本ElementTree实现受到一些限制。如果安装了lxml您将获得更强大的实现,它对XPath的支持要优越得多:

from lxml import etree as ET

document = ET.parse(r'C:\Users\ADMIN\Desktop\Abhishek\PSG - Copy\Sample.xml')
root = document.getroot()

qualifier = root.xpath(".//Event/Q[@id='142']")[0]
event = qualifier.getparent()
type_id = event.attrib["type_id"]
team_id = event.attrib["team_id"]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么 javascript 'For' 语句不能在数组中工作?

来自分类Dev

为什么Bash`(())`不能在`[[]]`中工作?

来自分类Dev

TextView 不能正常工作 - 为什么会这样?

来自分类Java

为什么我不能在Java中传递这样的数组

来自分类Dev

线程停止工作后,为什么不能在main()中运行语句?

来自分类Dev

为什么php文件不能在CLI中工作,而不能在浏览器中工作?

来自分类Dev

为什么我不能在 Java 中使用像 if-else 语句这样的 (? :) 运算符?

来自分类Dev

为什么不能在angularJs ng模式中工作?

来自分类Dev

为什么我的线性渐变不能在Firefox中工作?

来自分类Dev

为什么占位符不能在 tensorflow 中工作?

来自分类Dev

为什么我的硒不能在python脚本中工作?

来自分类Dev

为什么move方法不能在Java中工作?

来自分类Dev

如果长格式有效,如果此语句返回0,为什么会这样呢?

来自分类Dev

为什么这个 Megaparsec 解析器可以在 GHCi 中工作,但不能在源文件中编译?

来自分类Dev

为什么此模板只能在单个文件中工作,而不能在多个文件中工作?

来自分类Java

为什么这个素数会这样工作?

来自分类Dev

为什么我的 else/if 语句在 javascript 中不能正常工作?

来自分类Dev

当我检查相等性时,为什么每个都不能在 if 语句中工作?

来自分类Dev

如果不是语句不能在Excel VBA中的循环内正常工作

来自分类Dev

为什么不能在VBA中从这样的列表框中选择变量?

来自分类Dev

为什么我不能在 JavaScript 中编写这样的异步代码?

来自分类Dev

为什么我在If语句中设置的变量不能在javascript中的if语句之外起作用?

来自分类Dev

如果使用 FilterRegistrationBean,为什么 Autowired 不能在 Filter 内部工作?

来自分类Javascript

为什么此下拉列表可在Internet Explorer中工作但不能在Chrome中工作?

来自分类Dev

为什么此ctypes代码不能在Python 3.3中工作,而在Python 2.7中工作?

来自分类Dev

为什么这个SFINAE代码段不能在g ++中工作,而可以在MSVC中工作?

来自分类Dev

为什么jQuery hover可以在jsfiddle中工作,但不能在我的html布局中工作?

来自分类Dev

为什么 Unity (DI) 可以在 Controller 中工作,而不能在我的服务层中工作?

来自分类Dev

为什么 php 可以在 127.0.0.1 中工作,但不能在 localhost 中工作

Related 相关文章

  1. 1

    为什么 javascript 'For' 语句不能在数组中工作?

  2. 2

    为什么Bash`(())`不能在`[[]]`中工作?

  3. 3

    TextView 不能正常工作 - 为什么会这样?

  4. 4

    为什么我不能在Java中传递这样的数组

  5. 5

    线程停止工作后,为什么不能在main()中运行语句?

  6. 6

    为什么php文件不能在CLI中工作,而不能在浏览器中工作?

  7. 7

    为什么我不能在 Java 中使用像 if-else 语句这样的 (? :) 运算符?

  8. 8

    为什么不能在angularJs ng模式中工作?

  9. 9

    为什么我的线性渐变不能在Firefox中工作?

  10. 10

    为什么占位符不能在 tensorflow 中工作?

  11. 11

    为什么我的硒不能在python脚本中工作?

  12. 12

    为什么move方法不能在Java中工作?

  13. 13

    如果长格式有效,如果此语句返回0,为什么会这样呢?

  14. 14

    为什么这个 Megaparsec 解析器可以在 GHCi 中工作,但不能在源文件中编译?

  15. 15

    为什么此模板只能在单个文件中工作,而不能在多个文件中工作?

  16. 16

    为什么这个素数会这样工作?

  17. 17

    为什么我的 else/if 语句在 javascript 中不能正常工作?

  18. 18

    当我检查相等性时,为什么每个都不能在 if 语句中工作?

  19. 19

    如果不是语句不能在Excel VBA中的循环内正常工作

  20. 20

    为什么不能在VBA中从这样的列表框中选择变量?

  21. 21

    为什么我不能在 JavaScript 中编写这样的异步代码?

  22. 22

    为什么我在If语句中设置的变量不能在javascript中的if语句之外起作用?

  23. 23

    如果使用 FilterRegistrationBean,为什么 Autowired 不能在 Filter 内部工作?

  24. 24

    为什么此下拉列表可在Internet Explorer中工作但不能在Chrome中工作?

  25. 25

    为什么此ctypes代码不能在Python 3.3中工作,而在Python 2.7中工作?

  26. 26

    为什么这个SFINAE代码段不能在g ++中工作,而可以在MSVC中工作?

  27. 27

    为什么jQuery hover可以在jsfiddle中工作,但不能在我的html布局中工作?

  28. 28

    为什么 Unity (DI) 可以在 Controller 中工作,而不能在我的服务层中工作?

  29. 29

    为什么 php 可以在 127.0.0.1 中工作,但不能在 localhost 中工作

热门标签

归档