4

I have a xml like this

<data>
    <B>Head1</B>
    <I>Inter1</I>
    <I>Inter2</I>
    <I>Inter3</I>
    <I>Inter4</I>
    <I>Inter5</I>
    <O>,</O>
    <B>Head2</B>
    <I>Inter6</I>
    <I>Inter7</I>
    <I>Inter8</I>
    <I>Inter9</I>
    <O>,</O>
    <O> </O>
</data>

and I want the XML to look like

 <data>
        <combined>Head1 Inter1 Inter2 Inter3 Inter4 Inter5</combined>,
        <combined>Head2 Inter6 Inter7 Inter8 Inter9</combined>
 </data>

I tried to get all values of "B"

for value in mod.getiterator(tag='B'):
    print (value.text)

Head1
Head2

for value in mod.getiterator(tag='I'):
    print (value.text)

Inter1 
Inter2 
Inter3 
Inter4 
Inter5
Inter6 
Inter7 
Inter8 
Inter9

Now How should I save the first iteration value to one tag and then the second one in diffrent tag. ie. How do make the iteration to start at tag "B" find all the tag "I" which are following it and then iterate again if I again find a tag "B" and save them all in a new tag.

tag "O" will always be present at the end

1 Answer 1

5

You can use ElementTree module from xml.etree:

from xml.etree import ElementTree

struct = """
<data>
{}
</data>
"""
def reformat(tree):
    root = tree.getroot()
    seen = []
    for neighbor in root.iter('data'):
        for child in neighbor.getchildren():
            tx = child.text
            if tx == ',':
                yield "<combined>{}<combined>".format(' '.join(seen))
                seen = []
            else:
                seen.append(tx)

with open('test.xml') as f:
    tree = ElementTree.parse(f)

print(struct.format(',\n'.join(reformat(tree))))

result:

<data>
<combined>Head1 Inter1 Inter2 Inter3 Inter4 Inter5<combined>,
<combined>Head2 Inter6 Inter7 Inter8 Inter9<combined>
</data>

Note that if you're not sure all the blocks are separated wit comma you can simply change the condition if tx == ',': according your file format. You can also check when the tx is started with 'Head' then if seen is not empty yield the seen and clear its content, otherwise append the tx and continue.

Sign up to request clarification or add additional context in comments.

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.