Abh*_*hek 5 python xml if-statement elementtree xml-parsing
我正在尝试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>
Run Code Online (Sandbox Code Playgroud)
我使用的代码很简单并且按预期工作。但是,当我尝试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")
Run Code Online (Sandbox Code Playgroud)
这是它产生的错误:
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.
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除if statement并且效果完美。但是,我确实需要设置一个条件来调用所有id_具有特定值的 s 。我已经尝试使用"142"以及142但问题仍然存在。究竟为什么会发生这种情况?
这是 getchildren() 方法已被弃用的警告。以下是如何在没有警告的情况下让孩子们现在
def goddamnit_what_are_my_kids_called(self, element):
for child in list(element):
print(child.tag)
Run Code Online (Sandbox Code Playgroud)
您看到的错误不是错误,而是警告。您可以忽略它们,使它们静音,或者通过不使用.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:
Run Code Online (Sandbox Code Playgroud)
该if测试不起作用,因为 XML 属性是字符串、文本,而不是整数值。测试字符串:
if id_ == "142":
print("val")
Run Code Online (Sandbox Code Playgroud)
您可能想要使用XPath 查询而不是循环遍历所有内容。ElementTree不过,Python 附带的基本实现有一点限制。如果您安装了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"]
Run Code Online (Sandbox Code Playgroud)