如何在elementtree的Element().tex中打印新行

2024-04-25 07:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在元素对象的.text中有这个

e = ET.Element('p')
e.text = "hello <br> world"
e.write("a.html")

似乎不像预期的那样有效。 它将天使方括号转换为&lt; 有办法解决这个问题吗?在


Tags: 对象textbrlt元素helloworldhtml
1条回答
网友
1楼 · 发布于 2024-04-25 07:17:53

您可以使用tail属性,我还没有测试过它,但它应该可以像您预期的那样工作:

e = ET.Element('p')
# you set the text to hello first
e.text = "hello "
# and you set a subelement with br, which is what you want
br = ET.SubElement(e, 'br')
# then using tail to append the text after br
br.tail = ' world'

...

希望这有帮助。在

相关问题 更多 >