Is there a Python module that can be used in the same way as Perl's Data::Dumper
module?
Edit: Sorry, I should have been clearer. I was mainly after a module for inspecting data rather than persisting.
BTW Thanks for the answers. This is one awesome site!
Data::Dumper has two main uses: data persistence and debugging/inspecting objects. As far as I know, there isn't anything that's going to work exactly the same as Data::Dumper.
I use pickle for data persistence.
I use pprint to visually inspect my objects / debug.
I think the closest you will find is the pprint module.
>>> l = [1, 2, 3, 4]
>>> l.append(l)
>>> d = {1: l, 2: 'this is a string'}
>>> print d
{1: [1, 2, 3, 4, [...]], 2: 'this is a string'}
>>> pprint.pprint(d)
{1: [1, 2, 3, 4, <Recursion on list with id=47898714920216>],
2: 'this is a string'}
Possibly a couple of alternatives: pickle, marshal, shelve.
I too have been using Data::Dumper for quite some time and have gotten used to its way of displaying nicely formatted complex data structures. pprint as mentioned above does a pretty decent job, but I didn't quite like its formatting style. That plus pprint doesn't allow you to inspect objects like Data::Dumper does:
Searched on the net and came across these:
https://gist.github.com/1071857#file_dumper.pyamazon
>>> y = { 1: [1,2,3], 2: [{'a':1},{'b':2}]}
>>> pp = pprint.PrettyPrinter(indent = 4)
>>> pp.pprint(y)
{ 1: [1, 2, 3], 2: [{ 'a': 1}, { 'b': 2}]}
>>> print(Dumper.dump(y)) # Dumper is the python module in the above link
{ 1: [ 1 2 3 ] 2: [ { 'a': 1 } { 'b': 2 } ] }
>>> print(Dumper.dump(pp))
instance::pprint.PrettyPrinter __dict__ :: { '_depth': None '_stream': file:: > '_width': 80 '_indent_per_level': 4 }
Also worth checking is http://salmon-protocol.googlecode.com/svn-history/r24/trunk/salmon-playground/dumper.py It has its own style and seems useful too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With