Python pickle to xml

本小妞迷上赌 提交于 2019-12-04 21:34:43

gnosis.xml does support pickling to XML:

import gnosis.xml.pickle
xml_str = gnosis.xml.pickle.dumps(obj)

To deserialize the XML, use loads:

o2 = gnosis.xml.pickle.loads(xml_str)

Of course, this will not directly convert existing pickles to XML — you have to first deserialize them into live object, and then dump them to XML.

Having said that, I must warn you that gnosis.xml is quite slow, somewhat fragile, and most likely unmaintained (last release was over six years ago). It is also very bloated, containing a huge number of subpackages with lots and lots of features that not only you won't need, but are untested and buggy. We tried to use for our development and, after a lot of effort wasted on trying to debug and improve it, ended up writing a simple XML pickler running at ~500 lines of code, and never looked back.

First you need to unpickle the data by pickle.load or pickle.loads. Then generate xml snippet. If you have a pickle in tmpStr variable, simply do this:

c = pickle.loads(tmpStr)
print '<Coordinate>\n<x>%d</x>\n<y>%d</y>\n</Coordinate>' % (c.x, c.y)

Writing to file is left as an exercise to the reader.

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