views:

1655

answers:

6

Hi, i need to store Python structures made of lists / dictionaries, tuples into a human readable format. The idea is like using something similar to pickle, but pickle is not human-friendly. Other options that come to my mind are YAML (through PyYAML and JSON (through simplejson) serializers.

Any other option that comes to your mind?

Thanks in advance.

+6  A: 

For simple cases pprint() and eval() come 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 twice about fixing two requirements with the same tool. Have you considered using pickle for the serializing and then pprint() (or a more fancy object viewer) for humans looking at the objects?

PEZ
Well, i have to store those structures into files, so they're mainly for the program to load / save them, but i want them to be editable / readable by human beings as well.
pistacchio
+9  A: 

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

Caution: Tuples will be converted to lists in simplejson.

In [109]: simplejson.loads(simplejson.dumps({'d':(12,3,4,4,5)}))
Out[109]: {u'd': [12, 3, 4, 4, 5]}
JV
+1  A: 

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 being:

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

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

You can further pretty print 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}}
Soviut
A: 

The pprint solution may be what i'm after, but how to handle multiple nested levels? like:

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

Paramether depth seems not to work here.

pistacchio
If pprint doesn't work, perhaps you should use simplejson or pyyaml.
S.Lott
+1  A: 

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

Matthew Trevor
A: 

You should check out jsonpickle (http://code.google.com/p/jsonpickle/). It will write out any python object into a json file. You can then read that file back into a python object. The nice thing is the inbetween file is very readable because it's json.

Paul Hildebrandt