简体   繁体   中英

Getting root handle in xml iterparse in python

I want to know if getting the root handle by the following method is valid, using python 3.6. (It seems it is working for 2.7 with xml.etree.cElementTree). The data file used is at https://docs.python.org/3.4/library/xml.etree.elementtree.html#parsing-xml

# python 3.6
import xml.etree.ElementTree as ET
iterator = ET.iterparse(filename) # see my own response !
_, root = next(iterator)  
print('ROOT:', root.tag)  # prints 'rank', 1st tag, not 'data', root !!
for event, elem in iterator:  
    print(elem.tag, elem.attrib, elem.text) # prints 'year', 2nd element
    # process..
    elem.clear() # works, but empty tags accumulate ?
    root.clear() # doesn't clear

The code without root.clear() works, but the memory keeps going up (not fast though). I am asking the question because I see old code using it, and it would be nice to see an update for the latest python, so as to avoid making mistake.

Thanks

It is my mistake. It works with,

iterator = ET.iterparse(filename, events=("start", "end"))

and then process if "end".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM