6

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.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
lionel319
  • 1,180
  • 2
  • 17
  • 31
  • 3
    `pickle` is already highly questionable, and the idea of human-editing a pickle makes me want to run for the hills. what is this data and why do you want to pickle+edit it? – Eevee Mar 01 '13 at 02:01
  • OTOH pickle can dump ASCII streams (still not editable of course) – wRAR Mar 01 '13 at 02:03
  • 1
    You wouldn't edit a pickle. The Python source is already readable and editable. Why not use the source code? – Keith Mar 01 '13 at 02:06
  • It doesn't necessary needs to be using pickle. What i am actually looking for is a solution to be able to dump an python object, and later on loads in all the configuration into python. Is there any other way around this? – lionel319 Mar 01 '13 at 02:22
  • I would say a better title would be "Is there a python library as powerful as pickle but whose exported files are readable and editable?" I do not believe it should be some version of pickle neither it seems @lionel319 really _requires_ ASCII instead of e.g. UTF-8, but only a non-binary output. (Also, remember: [There Ain't No Such Thing As Plain Text](http://www.joelonsoftware.com/articles/Unicode.html).) – brandizzi Mar 01 '13 at 10:57

3 Answers3

5

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

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • So [json](http://docs.python.org/2/library/json.html) or [json](http://docs.python.org/3.3/library/json.html) – President James K. Polk Mar 01 '13 at 02:02
  • From RFC 4627 "JSON text SHALL be encoded in Unicode. The default encoding is UTF-8." So it's not ascii. – Keith Mar 01 '13 at 02:03
  • 2
    However, json only serializes data, not objects. YOu can't get a class instance back, for example. – Keith Mar 01 '13 at 02:05
  • @Keith in practice, JSON tends to be ASCII, since any non-ASCII characters must appear in string literals and encoders tend to write them out as escape sequences – Eevee Mar 01 '13 at 02:05
  • @Eevee But the spec says it's UTF-8, so you should expect to decode it as UTF-8. – Keith Mar 01 '13 at 02:07
  • I think json only allows simple text right? I'm looking more on dumping the entire python objects (including customized classes and stuff) into a file, and reloadable in the future. Can json do that? – lionel319 Mar 01 '13 at 02:20
  • Or yaml. It's not as popular as json, but it feels more pythonic (maybe if it had been named jaml it would be wildly overused and abused as json sometimes is). – Travis Griggs Mar 01 '13 at 03:23
  • @lionel319 It is possible to serialize objects of your classes to json, see here http://stackoverflow.com/questions/3768895/python-how-to-make-a-class-json-serializable – piokuc Mar 01 '13 at 10:18
  • @TravisGriggs json is a subset of YAML, see here: http://docs.python.org/2/library/json.html – piokuc Mar 01 '13 at 10:19
  • But could JSON serialize e.g. self-referring objects? I _believe_ it cannot (but I may be wrong). – brandizzi Mar 01 '13 at 10:28
  • @brandizzi I think it could be done by customizing JSONEncoder and Decoder. You would need to introduce a way to represent objects' identities and references to objects already serialized in order to deal with cycles. – piokuc Mar 01 '13 at 10:38
3

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()
lionel319
  • 1,180
  • 2
  • 17
  • 31
1

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.)

brandizzi
  • 26,083
  • 8
  • 103
  • 158