【发布时间】:2016-03-06 08:44:45
【问题描述】:
是否可以在构造函数中传递子元素的文本属性?我来描述一下。我想将第二行和第三行合并为一行:
import xml.etree.ElementTree as ET
son = ET.SubElement(parent, tagName)
son.text = 'some string'
提前致谢
【问题讨论】:
是否可以在构造函数中传递子元素的文本属性?我来描述一下。我想将第二行和第三行合并为一行:
import xml.etree.ElementTree as ET
son = ET.SubElement(parent, tagName)
son.text = 'some string'
提前致谢
【问题讨论】:
我将此标记为与How to set ElementTree Element text field in the constructor 重复。
简而言之,不,构造函数不支持它。您可以编写自定义函数:
def text_element(parent, tag, text, *args, **kwargs):
element = ET.SubElement(parent, tag, *args, **kwargs)
element.text = text
return element
【讨论】: