python - 为什么 if 语句在 ElementTree 解析中不起作用?

标签 python xml if-statement xml-parsing elementtree

我正在尝试使用如下所示的 ElementTree 解析 xml 文件:

<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 条件 时,一切都变了

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 语句 并且效果很好。但是,我确实需要设置一个条件来调用所有具有特定值的 id_。我试过使用 "142"142 但问题仍然存在。为什么会发生这种情况?

最佳答案

这是警告 getchildren() 方法已贬值。以下是如何在没有警告的情况下立即获取 child

def goddamnit_what_are_my_kids_called(self, element):
    for child in list(element):
        print(child.tag)

关于python - 为什么 if 语句在 ElementTree 解析中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55883322/

相关文章:

.net - 使用 mono 提高 .Net XML 序列化程序的性能

sql-server - SQL Server : How do I check if an element attribute of an xml node contains a specific string?

c# - If 语句的更好语法

java - 无法理解带有 else if 语句的 java 数学类(下面的代码)

找不到 linkedin oauth2 的 python-social-auth 后端

Python 类中的变量列表

java - 默认 XML namespace 、JDOM 和 XPath

javascript条件未到达else

python - Django查询多次出现的外来对象

python - 如何使用 Beautifulsoup 基于嵌套标签来切片和重新组合文本?