3

I have huge xml file with many tags. I need to extract one content of one tag and write it into other xml file using python code. The below are a sample of xml code:

<madamira_output xmlns="urn:edu.columbia.ccls.madamira.configuration:0.1">
     <out_seg id="SENT1">
           <word1>.....</word1>
           <word2>.....</word2>
     </out_seg>
     <out_seg id="SENT2">
           <word1>.....</word1>
           <word2>.....</word2>
     </out_seg>

And the below is python code:

 from xml.etree import ElementTree
 with open('100.xml', 'rt') as f:
     tree = ElementTree.parse(f)
 sent=[]
 for node in tree.iter('{urn:edu.columbia.ccls.madamira.configuration:0.1}out_seg'):
     sent.append(node)
 count_file=1
 for i in sent:
     save_path = '/Desktop/13/out'
     completeName = os.path.join(save_path, str(count_file)+".xml")
     count_file=count_file+1
     with open(completeName, "w") as f:
         f.write(i)

But nothing is written in files. Please help

1 Answer 1

1

subelements format of out_seg is incorrect. the end tag (word1) doesn't match the start tag(word_1).

second, using following code to write a element content into a file:

with open(completeName, "w") as f:

     f.write(ElementTree.tostring(i))
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.