Lei  Wu
Lei Wu

Reputation: 29

python print like this

I would like to print the following dictionary:

'test.com': {'@': {'NS': ['ns1.test.net', 'ns2.test.net']},
             'api': {'A': ['123.122.2.1','121.161.51.29','111.30.12.14']}}

Like the following:

'test.com': {
    '@': {
        'NS': ['ns1.test.net', 'ns2.test.net']
         },
    'api': {
        'A': ['123.122.2.1','121.161.51.29','111.30.12.14']
         }
 }

Thanks very much!

Upvotes: 1

Views: 89

Answers (2)

TerryA
TerryA

Reputation: 59974

Simple solution with json:

>>> import json
>>> data = {'test.com': {'@': {'NS': ['ns1.test.net', 'ns2.test.net']}, 'api': {'A': ['123.122.2.1', '121.161.51.29', '111.30.12.14']}}}
>>> print json.dumps(data,indent=4)
{
    "test.com": {
        "@": {
            "NS": [
                "ns1.test.net", 
                "ns2.test.net"
            ]
        }, 
        "api": {
            "A": [
                "123.122.2.1", 
                "121.161.51.29", 
                "111.30.12.14"
            ]
        }
    }
}

Upvotes: 2

Tim Bender
Tim Bender

Reputation: 20442

You can try the pprint module.

If you don't like the format the pprint provides, then you will need to write your own method for printing the object.

Upvotes: 0

Related Questions