Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/elementtree xml parsing into an array

Starting here:

<Program>

    <ManyTag>
        <InstSpecific Inst="FAMU" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="FAU" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="FIU" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="UCF" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="UF" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="UNF" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="USF" PgmHrs="120" LimitedAccess="N"/>
        <InstSpecific Inst="UWF" PgmHrs="120" LimitedAccess="N"/>
        <OtherTags>stuff</OtherTags>
    <ManyTag>

<Program>

From above I have a nested grouping of tags that are unique in attrib but not in tag name. I need to keep this grouping but I cannot figure out how to append the 'Inst' attrib together in an array, document or magical container. Before someone starts hollerin' about code:

tree = etree.parse('some.xml')
root = tree.getroot()

inst = []


for element in root.iter():

    if element.tag == 'InstSpecific':
        inst.append(element.get('Inst')

This is a short, truncated version but I have tried many things and am nearly blind with rage. I am so frustrated I would do it by hand, but that's over 20,000 entries for the 'InstSpecific' alone. Please help.

like image 612
TypeSlow Avatar asked Feb 02 '26 09:02

TypeSlow


1 Answers

Use .findall() to fin all InstSpecific tags and get the Inst attributes from the .attrib:

inst = [element.attrib['Inst'] for element in root.findall('InstSpecific')]

If you need to group the list of Inst attribute values for every ManyTag tag:

[element.attrib['Inst']
 for many_tag in tree.findall('ManyTag')
 for element in many_tag.findall('InstSpecific')]
like image 96
alecxe Avatar answered Feb 03 '26 22:02

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!