python - How to read some contents of xml files and write them into a text file? -


i have following xml file, want read contents in <seg> , save them plain text file python. , used dom module.

<?xml version="1.0"?>  <mteval>    <tstset setid="default" srclang="any" trglang="trglang" sysid="sysid">      <doc docid="ntpmt-dev-2000/even1k.cn.seg.txt">        <seg id="1">therefore , can obtained having excellent properties ( stability , solubility of balance of crystal pharmaceutical compound not possible predict .</seg>        <seg id="3">compound ( ) preferably crystalline , in particular , has stability , solubility equilibrium , suitable industrial prepared type crystal preferred .</seg>        <seg id="4">method b included in catalyst such dmf , , in presence of compound of formula ( ii ) thionyl chloride or oxalyl chloride give acyl chloride , in presence of base of acid chloride alcohol ( iv ) ( o ) reaction of esterification .</seg>      </doc>    </tstset>  </mteval>

from xml.dom.minidom import parse import xml.dom.minidom  dom = xml.dom.minidom.parse(r"path_to_xml file") file = dom.documentelement seg = dom.getelementsbytagname("seg") item in seg:     sent = item.firstchild.data     print(sent,sep='')  file = open(r'file.txt','w') file.write(sent) file.close() 

while running above codes, print lines on screen successfully, file.txt has 1 line of last <seg> (seg id=4), want save sentences file. there wrong codes?

you're calling file.write(sent) once, open file before loop, , add following line code:

file = open(r'file.txt','w')  item in seg:     sent = item.firstchild.data     print(sent,sep='')     file.write(sent) // <---- line  file.close() 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -