【问题标题】:How to remove/edit element in XML file using Python如何使用python删除/编辑xml文件中的元素
【发布时间】:2021-12-28 09:13:38
【问题描述】:

我有以下 xml 文件:

<annotation>
    <folder>JPEGImages</folder>
    <filename>01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</filename>
    <path>D:\aVisionData\PVL Pilot Project\test\Annotation\JPEGImages\01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>601</width>
        <height>844</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>smallObject</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>329</xmin>
            <ymin>199</ymin>
            <xmax>376</xmax>
            <ymax>242</ymax>
        </bndbox>
    </object>
</annotation>

我想删除&lt;path&gt;,还想编辑&lt;source&gt; &lt;/source&gt;,如下所示

<annotation>
    <folder>JPEGImages</folder>
    <filename>01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</filename>
    <source>
        <database>objects</database>
        <annotation>custom</annotation>
        <image>custom</image>
    </source>
    <size>
        <width>601</width>
        <height>844</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>smallObject</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>329</xmin>
            <ymin>199</ymin>
            <xmax>376</xmax>
            <ymax>242</ymax>
        </bndbox>
    </object>
</annotation>

要删除&lt;path&gt;,我使用了以下代码:

import xml.etree.ElementTree as Et

file_path = os.path.join(inputAnnotationPath, annotation)
tr = Et.parse(file_path)
for element in tr.iter():
    for subElement in element:
        print(subElement)
        if subElement.tag == "path":
            se = subElement.get("path")
            element.remove(subElement)
tr.write(sys.stdout)

它运行良好,但无法删除 path。我应该做些什么来删除path 并修改source。请帮忙。谢谢

【问题讨论】:

  • 这对于 XSLT 来说似乎是一项不错的工作,它也可以从 Python 中运行。
  • 我同意@MartinHonnen
  • 我已经针对示例输入 XML 运行了您的代码,我可以看到 path 已被删除。我还看到 source 未修改,但没有逻辑/代码,所以我并不感到惊讶。我正在运行 3.8.9。

标签: python xml


【解决方案1】:

如果可以使用lxml就很简单了:

from lxml import etree
parser = etree.XMLParser(recover=True)
tr = etree.parse(file_path, parser=parser)

#select both <path> and <source> for removal
targets = [tr.xpath('//path')[0], tr.xpath('//source')[0]]

#select the destination for the new <source> element
destination = tr.xpath('//filename')[0]

#recreate <source>
new_source = """
     <source>
        <database>objects</database>
        <annotation>custom</annotation>
        <image>custom</image>
    </source>"""

#remove what needs to be removed
for target in targets:
    target.getparent().remove(target)

#insert the new <source> element
destination.addnext(etree.fromstring(new_source))

#save to file
with open("output.xml", "wb") as f:
    f.write(etree.tostring(tr))

【讨论】:

  • tr 在哪里声明?看起来应该是etree.parse() 的结果。而且,关于filename 什么也没说。
  • @ZachYoung 哎呀!不错的收获 - 已修复。
猜你喜欢
  • 2021-01-11
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
相关资源
最近更新 更多