Python 2.7 Etree/lxml 最小化

Python 2.7 Etree/lxml minimizing

我正在使用 lxml/Etree 解析和写入 XSD 文档。

我有基本结构

tree = ET.parse('file.xsd')
# do stuff
tree.write('output.xsd')

但在某些情况下标签会最小化,例如:

<Cars>
    <Car type="Chevy"></Car>
</Cars>

缩短为

<Cars>
    <Car type="Chevy"/>
</Cars>

有没有办法让 lxml/ETree 不这样做?我四处搜索,显然这是件好事 - 但就我而言,我希望结束标签保持原样。

我正在解析整棵树,进行调整并将其保存到另一个文件

使用write方法的方法参数。参数值,如果 htmlxml

例如

tree.write("output.xsd", method="html")

还有 pretty print 参数,其值为 True 或 False

例如

tree.write("output.xsd", method="html", pretty_print=True)

有可能的参数:

write(self, file, encoding=None, method="xml",
          pretty_print=False, xml_declaration=None, with_tail=True,
          standalone=None, compression=0,
          exclusive=False, with_comments=True, inclusive_ns_prefixes=None)