0

i have a question. How can i create list of a list from xml dataset in ElemenTree python? for example i have this dataset :

<data>
    <article>
        <author> Tony </author>
    </article>
    <article>
        <author> Andy </author>
        <author> John </author>
    </article>
    <article>
        <author> Paul </author>
        <author> leon </author>
    </article>
</data>

There is a specific function like :

tree = ET.parse('data.xml')
root = tree.getroot()

for author in root.iter('author'):
  print(author.text, author.attrib['pid'])

that would find and show all author in dataset in single list [Tony, Andy, John, Paul, Leon]. How can i improve those code above so i can get result author in list of s list [[Tony], [Andy, John], [Paul, Leon]]? Maybe theres is a specific function to perform this?

2 Answers 2

1

One liner can do the magic

import xml.etree.ElementTree as ET

xml = '''<data>
    <article>
        <author> Tony </author>
    </article>
    <article>
        <author> Andy </author>
        <author> John </author>
    </article>
    <article>
        <author> Paul </author>
        <author> leon </author>
    </article>
</data>'''
root = ET.fromstring(xml)
authors = [[aut.text for aut in art.findall('author')] for art in root.findall('./article')]
print(authors)

output

[[' Tony '], [' Andy ', ' John '], [' Paul ', ' leon ']]
Sign up to request clarification or add additional context in comments.

2 Comments

If we use those [[aut.text for aut in art.findall('author')] for art in root.findall('./article')] (still confusing to me), which for run first?
the article is the external loop and author is the internal loop
0

I found the solution. It's quite simple. Just create nested loop to iterate child from root and then child2 from child with specific tag.

author_connection = []

for child in root:
    
    conn = []
    author_connection.append(conn)

    for child2 in child.iter('author'):
        conn.append(child2.text)

And the output is

[[Tony], [Andy, John], [Paul, Leon]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.