3

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.

1 Answer 1

1

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')]
Sign up to request clarification or add additional context in comments.

6 Comments

yes, that does work in getting all of the 'Inst'. What I use above also works. don't think I'm doing a good job explaining. Here is what I am looking for in an array
yes, that does work in getting all of the 'Inst'. What I use above also works. don't think I'm doing a good job explaining. Here is what I am looking for in an array, generically speaking: array = [[Breed][Names]]. This would look like array = [[Poodle][Fluffy, LadyBug, Snickers]] The tag would like<Dog Breed="Poodle' Names="Fluffy"> There would be several nested instances of <Dog> and I cannot figure out how to put them in the array so they are associted with a nested cluster of <Dog>.
@TypeSlow okay, thanks, but what would the desired output be for the xml sample provided in the question?
Ok. Thanks. Here's what I want.: array = [['FAMU', 'FAU', 'FIU', 'UCF', 'UNF', 'USF', 'UWF']] Each subsequent row of the array would hold all of the unique 'inst' for that particular nesting of <'IntSpecific>. Thanks for your help!
@TypeSlow hope I've got what you meant, please see the update.
|

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.