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

Why is if statement not working in ElementTree parsing?

我正在尝试使用 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 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 但问题仍然存在。为什么会这样?

您看到的错误不是错误,而是warnings。您可以忽略它们、使它们静音或通过不使用 .getchildren() 来修复您的代码;您可以直接遍历每个 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 queries instead of looping over everything. The base ElementTree implementation that comes with Python is a little limited though. You would get a far more powerful implementation if you installed the lxml library, its XPath support 远远优于:

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"]

这是一条警告,getchildren() 方法已弃用。以下是如何在没有警告

的情况下立即获取 children
def goddamnit_what_are_my_kids_called(self, element):
    for child in list(element):
        print(child.tag)