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

后端 未结 2 1528
囚心锁ツ
囚心锁ツ 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()
    
    0 讨论(0)
  • 2021-02-10 04:45

    You can use elementtree.iterparse and discard each book tag after it is processed.

    0 讨论(0)
提交回复
热议问题