root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')
输出是:
<document>
<test/>
</document
我希望输出为:
<document>
<test>
</test>
</document>
我知道两者是等价的,但有没有办法获得我想要的输出。
最佳答案
将tostring
的method
参数设置为html
。如:
etree.tostring(root, method="html")
关于python - 覆盖 lxml 行为以编写 Null 标记的结束和开始元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543399/