1 回答

TA贡献2003条经验 获得超2个赞
当您从<Host>元素开始并按照自己的方式工作时,它相对简单。
迭代所有节点,但只在子字符串"Update status:"出现在 的值中时输出一些东西<output>:
for host in tree.iter("Host"):
host_id = host.find('./Properties/tag[@name="id"]')
host_os = host.find('./Properties/tag[@name="os"]')
host_ip = host.find('./Properties/tag[@name="ip"]')
for output in host.iter("output"):
if output.text is not None and "Update status:" in output.text:
print("id:" + host_id.text)
print("os:" + host_os.text)
print("ip:" + host_ip.text)
for line in output.text.splitlines():
if ("last detected:" in line or
"last downloaded" in line or
"last installed" in line):
print(line.strip())
为您的示例 XML 输出此内容:
id:1
os:windows
ip:1.11.111.1
last detected: 2015-12-02 18:48:28
last downloaded: 2015-11-17 12:34:22
last installed: 2015-11-23 01:05:32
次要问题:这不是真正的 CSV,因此将其按原样写入 *.csv 文件不会很干净。
添加回答
举报