[Python] Getting the "sibling" of a "child" in xml.etree.ElementTree, Python,

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to XML and python, I am struggling to get the "sibling" of a "child"

    I have this XML

    <notes>
    <Def id="1">
    <module>DDAC</module>
    <tags> lalala</tags>
    <description> John and Mark are good friends. </description>
    </Def>
    <Def id="2">
    <module>FYP</module>
    <tags> lelele</tags>
    <description> John works in Google. </description>
    </Def>
    <Def id="3">
    <module>FYP</module>
    <tags> lilili</tags>
    <description> Mark work in IBM. </description>
    </Def>
    <Def id="4">
    <module>DDAC</module>
    <tags> lololo</tags>
    <description> A computer can help you do stuff </description>
    </Def>
    <Def id="5">
    <module>IMNPD</module>
    <tags> lululu</tags>
    <description> The internet is a world wide web </description>
    </Def>
    </notes>


    I want to get the description for all the module "DDAC"

    for g in root.iter('module'):
    if g.text == 'DDAC':
    x = root.iter("description")
    print(x)


    my desire output is:


    John and Mark are good friends.


    A computer can help you do stuff
    ​

    but I am getting the object not the text

    Continue reading...

Compartilhe esta Página