XML parsing in Python for big data

柔情痞子 提交于 2019-12-04 05:28:52

问题


I am trying to parse an XML file using Python. But the problem is that the XML file size is around 30GB. So, it's taking hours to execute:

tree = ET.parse('Posts.xml')

In my XML file, there are millions of child elements of the root. Is there any way to make it faster? I don't need all the children to parse. Even the first 100,000 would be fine. All I need is to set a limit for the depth to parse.


回答1:


You'll want an XML parsing mechanism that doesn't load everything into memory.

You can use ElementTree.iterparse or you could use Sax.

Here is a page with some XML processing tutorials for Python.

UPDATE: As @marbu said in the comment, if you use ElementTree.iterparse be sure to use it in such a way that you get rid of elements in memory when you've finished processing them.



来源:https://stackoverflow.com/questions/28160018/xml-parsing-in-python-for-big-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!