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 afte
Here is a simple solution for dumping nested data made up of dictionaries, lists, or tuples (it works quite well for me):
Python2
def printStruct(struc, indent=0):
if isinstance(struc, dict):
print ' '*indent+'{'
for key,val in struc.iteritems():
if isinstance(val, (dict, list, tuple)):
print ' '*(indent+1) + str(key) + '=> '
printStruct(val, indent+2)
else:
print ' '*(indent+1) + str(key) + '=> ' + str(val)
print ' '*indent+'}'
elif isinstance(struc, list):
print ' '*indent + '['
for item in struc:
printStruct(item, indent+1)
print ' '*indent + ']'
elif isinstance(struc, tuple):
print ' '*indent + '('
for item in struc:
printStruct(item, indent+1)
print ' '*indent + ')'
else: print ' '*indent + str(struc)
Python3
def printStruct(struc, indent=0):
if isinstance(struc, dict):
print (' '*indent+'{')
for key,val in struc.items():
if isinstance(val, (dict, list, tuple)):
print (' '*(indent+1) + str(key) + '=> ')
printStruct(val, indent+2)
else:
print (' '*(indent+1) + str(key) + '=> ' + str(val))
print (' '*indent+'}')
elif isinstance(struc, list):
print (' '*indent + '[')
for item in struc:
printStruct(item, indent+1)
print (' '*indent + ']')
elif isinstance(struc, tuple):
print (' '*indent + '(')
for item in struc:
printStruct(item, indent+1)
print (' '*indent + ')')
else: print (' '*indent + str(struc))
See it at work:
>>> d = [{'a1':1, 'a2':2, 'a3':3}, [1,2,3], [{'b1':1, 'b2':2}, {'c1':1}], 'd1', 'd2', 'd3']
>>> printStruct(d)
[
{
a1=> 1
a3=> 3
a2=> 2
}
[
1
2
3
]
[
{
b1=> 1
b2=> 2
}
{
c1=> 1
}
]
d1
d2
d3
]
I needed to return Perl-like dump for API request so I came up with this which doesn't format the output to be pretty, but makes perfect job for me.
from decimal import Decimal
from datetime import datetime, date
def dump(self, obj):
if obj is None:
return "undef"
if isinstance(obj, dict):
return self.dump_dict(obj)
if isinstance(obj, (list, tuple)):
return self.dump_list(obj)
if isinstance(obj, Decimal):
return "'{:.05f}'".format(obj)
# ... or handle it your way
if isinstance(obj, (datetime, date)):
return "'{}'".format(obj.isoformat(
sep=' ',
timespec='milliseconds'))
# ... or handle it your way
return "'{}'".format(obj)
def dump_dict(self, obj):
result = []
for key, val in obj.items():
result.append(' => '.join((self.dump(key), self.dump(val))))
return ' '.join(('{', ', '.join(result), '}'))
def dump_list(self, obj):
result = []
for val in obj:
result.append(self.dump(val))
return ' '.join(('[', ', '.join(result), ']'))
Using the above:
example_dict = {'a': 'example1', 'b': 'example2', 'c': [1, 2, 3, 'asd'], 'd': [{'g': 'something1', 'e': 'something2'}, {'z': 'something1'}]}
print(dump(example_dict))
will ouput:
{ 'b' => 'example2', 'a' => 'example1', 'd' => [ { 'g' => 'something1', 'e' => 'something2' }, { 'z' => 'something1' } ], 'c' => [ '1', '2', '3', 'asd' ] }
Saw this and realized Python has something that works akin to Data::Dumper in Dumper. The author describes it as
Dump Python data structures (including class instances) in a nicely- nested, easy-to-read form. Handles recursive data structures properly, and has sensible options for limiting the extent of the dump both by simple depth and by some rules for how to handle contained instances.
Install it via pip. The Github repo is at https://github.com/jric/Dumper.py.
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.
As far as inspecting your object goes, I found this a useful equivalent of Data:Dumper:
https://salmon-protocol.googlecode.com/svn-history/r24/trunk/salmon-playground/dumper.py
It can handle unicode strings.