Iterating XML with lxml in Python: how to know how much of the input file has been read?
You could pass a file object to iterparse
, and then call f.tell()
.
This will give you the approximate position of the Element in the file.
import lxml.etree as ET
import os
filename = 'data.xml'
total_size = os.path.getsize(filename)
with open(filename, 'r') as f:
context = ET.iterparse(f, events=('end', ), tag='Record')
for event, elem in context:
print(event, elem, float(f.tell())/total_size)
will yield something like
(u'end', <Element Record at 0xb743e2d4>, 0.09652665470688218)
(u'end', <Element Record at 0xb743e2fc>, 0.09652665470688218)
(u'end', <Element Record at 0xb743e324>, 0.09652665470688218)
...
(u'end', <Element Record at 0xb744739c>, 1.0)
(u'end', <Element Record at 0xb74473c4>, 1.0)
(u'end', <Element Record at 0xb74473ec>, 1.0)