saving an 'lxml.etree._ElementTree' object

回眸只為那壹抹淺笑 提交于 2019-11-28 09:57:55

You are already dealing with XML, and lxml is great at parsing XML. So I think the simplest thing to do would be to serialize to XML:

To write to file:

import lxml.etree as ET

filename = '/tmp/test.xml'
myobject.write(filename)

To call the write method, note that myobject must be an lxml.etree._ElementTree. If it is an lxml.etree._Element, then you would need myobject.getroottree().write(filename).

To parse from file name/path, file object, or URL:

myobject = ET.parse(file_or_url)

To parse from string:

myobject = ET.fromstring(content)

lxml is a C library - libxml to be precise - and the object probably don't support python pickling or any other kind of serialization - except serializing them to XML.

So you'll either have to keep them in memory, or re-parse the XML fragments you need, I assume.

I don't believe you can pickle lxml instances, but what I did because I was in a similar situation was I pickled the object instances that would build the tree.

Each instance and its child had a function to build the Element tree. So I would simply pickle/cache the Python object, fetch it from cache, and then call the build functions to get my Element tree.

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