【问题标题】:Adding subelement text in element constructor using ElementTree [duplicate]使用 ElementTree 在元素构造函数中添加子元素文本
【发布时间】:2016-03-06 08:44:45
【问题描述】:

是否可以在构造函数中传递子元素的文本属性?我来描述一下。我想将第二行和第三行合并为一行:

import xml.etree.ElementTree as ET
son = ET.SubElement(parent, tagName) 
son.text = 'some string'

提前致谢

【问题讨论】:

    标签: python xml


    【解决方案1】:

    我将此标记为与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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多