using lxml and iterparse() to parse a big (+- 1Gb) XML file

后端 未结 3 2074
我寻月下人不归
我寻月下人不归 2020-11-27 17:25

I have to parse a 1Gb XML file with a structure such as below and extract the text within the tags \"Author\" and \"Content\":


    

        
相关标签:
3条回答
  • 2020-11-27 17:38
    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
        for child in element:
            print child.tag, child.text
        element.clear()
    

    the final clear will stop you from using too much memory.

    [update:] to get "everything between ... as a string" i guess you want one of:

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
        print etree.tostring(element)
        element.clear()
    

    or

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
        print ''.join([etree.tostring(child) for child in element])
        element.clear()
    

    or perhaps even:

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
        print ''.join([child.text for child in element])
        element.clear()
    
    0 讨论(0)
  • 2020-11-27 18:01

    For future searchers: The top answer here suggests clearing the element on each iteration, but that still leaves you with an ever-increasing set of empty elements that will slowly build up in memory:

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
        for child in element:
            print child.tag, child.text
        element.clear()
    

    ^ This is not a scalable solution, especially as your source file gets larger and larger. The better solution is to get the root element, and clear that every time you load a complete record. This will keep memory usage pretty stable (sub-20MB I would say).

    Here's a solution that doesn't require looking for a specific tag. This function will return a generator that yields all 1st child nodes (e.g. <BlogPost> elements) underneath the root node (e.g. <Database>). It does this by recording the start of the first tag after the root node, then waiting for the corresponding end tag, yielding the entire element, and then clearing the root node.

    from lxml import etree
    
    xmlfile = '/path/to/xml/file.xml'
    
    def iterate_xml(xmlfile):
        doc = etree.iterparse(xmlfile, events=('start', 'end'))
        _, root = next(doc)
        start_tag = None
        for event, element in doc:
            if event == 'start' and start_tag is None:
                start_tag = element.tag
            if event == 'end' and element.tag == start_tag:
                yield element
                start_tag = None
                root.clear()
    
    0 讨论(0)
  • 2020-11-27 18:02

    I prefer XPath for such things:

    In [1]: from lxml.etree import parse
    
    In [2]: tree = parse('/tmp/database.xml')
    
    In [3]: for post in tree.xpath('/Database/BlogPost'):
       ...:     print 'Author:', post.xpath('Author')[0].text
       ...:     print 'Content:', post.xpath('Content')[0].text
       ...: 
    Author: Last Name, Name
    Content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.
    Author: Last Name, Name
    Content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.
    Author: Last Name, Name
    Content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dictum dictum vehicula.
    

    I'm not sure if it's different in terms of processing big files, though. Comments about this would be appreciated.

    Doing it your way,

    for event, element in etree.iterparse(path_to_file, tag="BlogPost"):
         for info in element.iter():
             if info.tag in ('Author', 'Content'):
                 print info.tag, ':', info.text
    
    0 讨论(0)
提交回复
热议问题