0
我有这个在我的元素对象的.text如何打印新的生产线的ElementTree的Element()。文字
e = ET.Element('p')
e.text = "hello <br> world"
e.write("a.html")
似乎并不如预期的工作。 它将天使括号转换为<
; 有什么办法可以解决这个问题?
我有这个在我的元素对象的.text如何打印新的生产线的ElementTree的Element()。文字
e = ET.Element('p')
e.text = "hello <br> world"
e.write("a.html")
似乎并不如预期的工作。 它将天使括号转换为<
; 有什么办法可以解决这个问题?
您可以使用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'
...
希望这有助于。
它的工作原理。谢谢。 – lionel319 2014-11-25 06:24:43