0

我正在使用 Elementtree 解析 xml 文件,编辑内容并写入新的 xml 文件。我把这一切都从一个问题中分离出来。当我生成文件时,有很多额外的行包含命名空间信息。以下是一些代码片段:

import xml.etree.ElementTree as ET
ET.register_namespace("", "http://clish.sourceforge.net/XMLSchema")
tree = ET.parse('ethernet.xml')
root = tree.getroot()

commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
                        'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')

for command in commands:
    all1.append(list(command.iter()))

以及带有错误行的输出文件示例xmlns="http://clish.sourceforge.net/XMLSchema

<COMMAND xmlns="http://clish.sourceforge.net/XMLSchema" help="Interface specific description" name="description">
            <PARAM help="Description (must be in double-quotes)" name="description" ptype="LINE" />
            <CONFIG />
</COMMAND>

如何使用 elementtree 删除它,可以吗?还是我必须使用一些正则表达式(我正在将字符串写入文件)?

4

0 回答 0