Is there a Python equivalent to Perl's Data::Dumper for inspecting data structures?

前端 未结 11 1344
小鲜肉
小鲜肉 2020-12-10 00:19

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

11条回答
  •  再見小時候
    2020-12-10 00:56

    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.

提交回复
热议问题