【发布时间】:2020-07-22 21:23:05
【问题描述】:
我想检索特定的标签属性。
file 标签包含子标签 filename 并基于此字段我想决定是否应采用 modification。
换句话说:如果filename值包含.tar我想打印修改时间。
在下面的示例中,我希望 2020-07-15T06:41:12.000Z 会被打印出来。
我尝试这样做了 2 个小时,但没有成功,因此我将非常感谢任何让我更接近解决方案的提示。
这是代码,但没有任何内容被打印或添加到dates 列表中:
import xml.etree.ElementTree as ET
tree = ET.parse(r"C:\path\to\file\logs.xml")
root = tree.getroot()
dates = []
for filetag in root.findall('.//{*}file'):
for filename in filetag.findall('../{*}filename'):
if ".tar" in filename.attrib['value']:
print(filename)
dates.append(filename)
这是 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="user@11.11.111.11" start="2020-07-22T10:01:12.939Z">
<ls>
<destination value="/folder/processing" />
<files>
<file>
<filename value="." />
<type value="d" />
<modification value="2020-07-22T08:57:28.000Z" />
<permissions value="rwxrwsrwx" />
<owner value="1000130000" />
<group value="0" />
</file>
<file>
<filename value=".." />
<type value="d" />
<modification value="2020-07-22T08:51:15.000Z" />
<permissions value="rwxrwxrwx" />
<owner value="1000130000" />
<group value="0" />
</file>
<file>
<filename value="package_tsp200715092001_20200715074120.tar" />
<type value="-" />
<size value="4014536192" />
<modification value="2020-07-15T06:41:12.000Z" />
<permissions value="rw-rw-rw-" />
<owner value="1005" />
<group value="1005" />
</file>
<file>
<filename value="package_tsp200715092001_20200715074120" />
<type value="d" />
<modification value="2020-07-15T06:41:59.000Z" />
<permissions value="rwxr-Sr--" />
<owner value="1000130000" />
<group value="0" />
</file>
</files>
<result success="true" />
</ls>
</session>
【问题讨论】:
标签: python xml elementtree