How to split an XML file the simple way in Python?

后端 未结 2 1529
囚心锁ツ
囚心锁ツ 2021-02-10 03:43

I have Python code for parsing an XML file as detailed here. I understand that XML files are notorious for hogging system resources when manipulated in memory. My solution works

2条回答
  •  囚心锁ツ
    2021-02-10 04:36

    You can parse your big XML file incrementally:

    from xml.etree.cElementTree import iterparse
    
    # get an iterable and turn it into an iterator
    context = iter(iterparse("path/to/big.xml", events=("start", "end")))
    
    # get the root element
    event, root = next(context)
    assert event == "start"
    
    for event, elem in context:
        if event == "end" and elem.tag == "book":
           # ... process book elements ...
           root.clear()
    

提交回复
热议问题