gpt4 book ai didi

python - 覆盖 lxml 行为以编写 Null 标记的结束和开始元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:02:56 25 4
gpt4 key购买 nike

root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')

输出是:

<document>
<test/>
</document

我希望输出为:

<document>
<test>
</test>
</document>

我知道两者是等价的,但有没有办法获得我想要的输出。

最佳答案

tostringmethod 参数设置为html。如:

etree.tostring(root, method="html")

引用:Close a tag with no text in lxml

关于python - 覆盖 lxml 行为以编写 Null 标记的结束和开始元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com