Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python pickle equivalent library which dumps out ascii instead of binary stream?

The output from pickle is non-human readable, and thus, non editable.

I'm looking for something which can do exactly the same (or very close to) pickle, whereby it can dump out all the python understandable objects into a file, and later on be able to directly load it back.

like image 451
lionel319 Avatar asked Mar 01 '13 01:03

lionel319


3 Answers

Look no further, go for json. json is a text format and can be edited easily. Out of the box you can use it for serializing objects of Python's built-in types like lists, dictionaries, strings, etc., but there are ways to serialize objects of your own classes to json as well, see here: How to make a class JSON serializable

like image 103
piokuc Avatar answered Sep 22 '22 22:09

piokuc


hmmm ........ I find that this somewhat kinda work for me.

Dumping Object to File

import pprint
f = open('a.txt', 'w')
pprint.pprint(myobject, f)
f.close()

Loading object from file

import pprint
f = open('a.txt')
lines = f.read()
myobject = eval(lines)
f.close()
like image 26
lionel319 Avatar answered Sep 22 '22 22:09

lionel319


If you want to have all the flexibility of pickle, I would say the most sensible thing to do is to create an specific format which can help you.

I do not know any tool which would be as powerful as pickle and yet would generate editable exported data, but you can create your own. Some time ago, I created a toy module which would wrap a Python module so it could send and receive XML messages, mostly to show to a friend how it is easy to work with Python :) It is not exactly what you are looking for but can be a starting point so take a look at it.

(BTW, I would be happy to know some other tools which do the same thing, in a better way. It should exist, I believe I just didn't find it yet.)

like image 31
brandizzi Avatar answered Sep 23 '22 22:09

brandizzi