【问题标题】:how to retrieve specific tag information using ElementTree?如何使用 ElementTree 检索特定的标签信息?
【发布时间】: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


    【解决方案1】:
    for filename in filetag.findall('../{*}filename'):
    

    由于..,这会在file 元素的 中查找filename(即作为file 的兄弟)。应该是单个.

    此外,在 Python 3.8 中添加了 namespace 通配符。您没有指明您使用的是哪个 Python 版本,因此这也可能是一个问题。

    无论如何,“正确”使用命名空间而不是寻找快捷方式可能会更好,这有点冗长但几乎没有困难

    NS = {'scp': 'http://winscp.net/schema/session/1.0'}
    for filetag in root.findall('.//scp:file', NS):
        for filename in filetag.findall('./scp:filename', NS):
            if ".tar" in filename.get('value', ''):
                print(filename)
                dates.append(filename)
    

    【讨论】:

    • 我刚刚注意到,我犯了一个错误,保存 filename 而不是 modification。在这种情况下,我应该如何编辑 print(filename) 以打印正确的日期?换句话说:如果filename(即modification)的兄弟姐妹包含.tar,如何打印父filemodification标签?
    • 您可以使用Element.find 获取file 元素的modification 子元素。事实上,你也可以为 filename 这样做,因为只有一个,不需要迭代。
    【解决方案2】:

    下面是一个单行:

    import xml.etree.ElementTree as ET
    
    xml = '''
    
    <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>
    '''
    
    NS = {'scp': 'http://winscp.net/schema/session/1.0'}
    root = ET.fromstring(xml)
    tar_files_dates = [f.find('./scp:modification',NS).attrib['value'] for f in root.findall('.//scp:file',NS) if '.tar' in f.find('./scp:filename',NS).attrib['value']]
    print(tar_files_dates)
    

    输出

    ['2020-07-15T06:41:12.000Z']
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2015-05-05
      相关资源
      最近更新 更多