How to pretty-print cyclic dictionaries?

Garrick P garrickp at gmail.com
Thu Apr 29 14:09:13 EDT 2010


Chris Rebert <clp2 <at> rebertia.com> writes:

...

> If you want a prettier print, you could try serializing it to YAML and
> printing the result out; YAML has syntax for "tags".
> 
> Cheers,
> Chris
> --
> http://blog.rebertia.com

Works fairly well.

$ python
Python 2.6.4 (r264:75706, Mar  1 2010, 14:28:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>> a = {}
>>> a['a'] = a
>>> a
{'a': {...}}
>>> print yaml.dump(a)
&id001
a: *id001
>>> b = {}
>>> b['b'] = b
>>> b['a'] = a
>>> a['b'] = b
>>> print yaml.dump(a)
&id001
a: *id001
b: &id002
  a: *id001
  b: *id002







More information about the Python-list mailing list