0

我想打印以下字典:

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

如下所示:

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

非常感谢!

4

2 回答 2

1

简单的解决方案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"
            ]
        }
    }
}
于 2013-03-20T07:05:59.493 回答
0

你可以试试pprint模块。

如果您不喜欢pprint提供的格式,那么您将需要编写自己的方法来打印对象。

于 2013-03-20T06:47:15.643 回答