[ACCEPTED]-Python human readable object serialization-serialization

Accepted answer
Score: 18

For simple cases pprint() and eval() come 6 to mind.

Using your example:

>>> d = {'age': 27,
...  'name': 'Joe',
...  'numbers': [1, 
...              2, 
...              3,
...              4,
...              5],
...  'subdict': {
...              'first': 1, 
...              'second': 2,
...               'third': 3
...              }
... }
>>> 
>>> from pprint import pprint
>>> pprint(d)
{'age': 27,
 'name': 'Joe',
 'numbers': [1, 2, 3, 4, 5],
 'subdict': {'first': 1, 'second': 2, 'third': 3}}
>>> 

I would think 5 twice about fixing two requirements with 4 the same tool. Have you considered using 3 pickle for the serializing and then pprint() (or 2 a more fancy object viewer) for humans looking 1 at the objects?

Score: 15

If its just Python list, dictionary and tuple 4 object. - JSON is the way to go. Its human readable, very 3 easy to handle and language independent 2 too.

Caution: Tuples will be converted to 1 lists in simplejson.

In [109]: simplejson.loads(simplejson.dumps({'d':(12,3,4,4,5)}))
Out[109]: {u'd': [12, 3, 4, 4, 5]}
Score: 7

You should check out jsonpickle (https://github.com/jsonpickle/jsonpickle). It 5 will write out any python object into a 4 json file. You can then read that file 3 back into a python object. The nice thing 2 is the inbetween file is very readable because 1 it's json.

Score: 4

If you're after more representations than 7 are covered by JSON, I highly recommend 6 checking out PyON (Python Object Notation)...although 5 I believe it's restricted to 2.6/3.0 and 4 above, as it relies on the ast module. It handles 3 custom class instances and recursive data 2 types, amongst other features, which is 1 more than is provided by JSON.

Score: 4

What do you mean this is not human-readable??? ;)

>>> d = {'age': 27, 
...   'name': 'Joe',
...   'numbers': [1,2,3,4,5],
...   'subdict': {'first':1, 'second':2, 'third':3}
... }
>>> 
>>> import pickle
>>> p = pickle.dumps(d)      
>>> p
"(dp0\nS'age'\np1\nI27\nsS'subdict'\np2\n(dp3\nS'second'\np4\nI2\nsS'third'\np5\nI3\nsS'first'\np6\nI1\nssS'name'\np7\nS'Joe'\np8\nsS'numbers'\np9\n(lp10\nI1\naI2\naI3\naI4\naI5\nas."

Ok, well, maybe 8 it just takes some practice… or you could 7 cheat...

>>> import pickletools 
>>> pickletools.dis(p)
    0: (    MARK
    1: d        DICT       (MARK at 0)
    2: p    PUT        0
    5: S    STRING     'age'
   12: p    PUT        1
   15: I    INT        27
   19: s    SETITEM
   20: S    STRING     'subdict'
   31: p    PUT        2
   34: (    MARK
   35: d        DICT       (MARK at 34)
   36: p    PUT        3
   39: S    STRING     'second'
   49: p    PUT        4
   52: I    INT        2
   55: s    SETITEM
   56: S    STRING     'third'
   65: p    PUT        5
   68: I    INT        3
   71: s    SETITEM
   72: S    STRING     'first'
   81: p    PUT        6
   84: I    INT        1
   87: s    SETITEM
   88: s    SETITEM
   89: S    STRING     'name'
   97: p    PUT        7
  100: S    STRING     'Joe'
  107: p    PUT        8
  110: s    SETITEM
  111: S    STRING     'numbers'
  122: p    PUT        9
  125: (    MARK
  126: l        LIST       (MARK at 125)
  127: p    PUT        10
  131: I    INT        1
  134: a    APPEND
  135: I    INT        2
  138: a    APPEND
  139: I    INT        3
  142: a    APPEND
  143: I    INT        4
  146: a    APPEND
  147: I    INT        5
  150: a    APPEND
  151: s    SETITEM
  152: .    STOP
highest protocol among opcodes = 0
>>> 

You'd still have to read the pickled 6 object from a file, however you wouldn't 5 need to load it. So, if it's a "dangerous" object, you 4 still might be able to figure that out before 3 doing the load. If you are stuck with a pickle, it 2 might be a good option for deciphering what 1 you have.

Score: 2

To use simplejson first easy_install simplejson:

import simplejson
my_structure = {"name":"Joe", "age":27, "numbers":[1,2,3,4,5], "subdict":{"first":1, "second":2, "third": 3}}
json = simplejson.dumps(my_structure)

results in json 5 being:

{"age": 27, "subdict": {"second": 2, "third": 3, "first": 1}, "name": "Joe", "numbers": [1, 2, 3, 4, 5]}

Notice that its hardly changed the 4 format of the dictionary at all, but you 3 should run it through this step to ensure 2 valid JSON data.

You can further pretty print 1 the result:

import pprint
pprint.pprint(my_structure)

results in:

{'age': 27,
 'name': 'Joe',
 'numbers': [1, 2, 3, 4, 5],
 'subdict': {'first': 1, 'second': 2, 'third': 3}}
Score: 0

There is AXON (textual) format that combine 7 the best of JSON, XML and YAML. AXON format 6 is quite readable and relatively compact.

The 5 python (2.7/3.3-3.7) module pyaxon supports load(s)/dump(s) functionality, including 4 iterative loading/dumping. It's sufficiently fast in order 3 to be useful.

Consider simple example:

>>> d = {
     'age': 27, 'name': 'Joe', 
     'numbers': [1, 2, 3, 4, 5], 
     'subdict': {'first': 1, 'second': 2, 'third': 3}
    }
# pretty form
>>> axon.dumps(d, pretty=1)
{ age: 27
  name: "Joe"
  numbers: [1 2 3 4 5]
  subdict: {
    first: 1
    second: 2
    third: 3}}
# compact form
>>> axon.dumps(d)
{age:27 name:"Joe" numbers:[1 2 3 4 5] subdict:{first:1 second:2 third:3}}

It 2 also can handle multiple objects in the 1 message:

>>> msg = axon.dumps([{'a':1, 'b':2, 'c':3}, {'a':2, 'b':3, 'c':4}])
>>> print(msg)
{a:1 b:2 c:3} 
{a:2 b:3 c:4}
{a:3 b:4 c:5}

and then load them iteratively:

for d in axon.iloads(msg):
   print(d)

More Related questions