代码之家  ›  专栏  ›  技术社区  ›  Sven K

无法访问XML数据结构中的“数据”:“名称属性>数据”

  •  0
  • Sven K  · 技术社区  · 2 年前

    我需要访问以下XML数据结构中的“数据”:

    <Name Attributes> Data </Name>

    使用常规Python解析 .attrib 我只能访问“属性”,但我需要“数据”。

    您知道这种数据结构的含义吗?我如何使用Python访问“数据”。

    非常感谢。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Justin Yu    2 年前

    根据你的例子,应该是 name.text 或者要为此分配新值 name.text = "new Data"

    例子:

    import xml.etree.ElementTree as ET
    tree = ET.parse("some-random-file")
    parent = tree.getroot()
    
    name = parent.find(".//name")
    name.text = "new Data"
    

    Reference