pretty-print json in python (pythonic way)

后端 未结 4 1945
闹比i
闹比i 2021-01-31 01:16

I know that the pprint python standard library is for pretty-printing python data types. However, I\'m always retrieving json data, and I\'m wondering if there is a

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 01:50

    Python's builtin JSON module can handle that for you:

    >>> import json
    >>> a = {'hello': 'world', 'a': [1, 2, 3, 4], 'foo': 'bar'}
    >>> print(json.dumps(a, indent=2))
    {
      "hello": "world",
      "a": [
        1,
        2,
        3,
        4
      ],
      "foo": "bar"
    }
    

提交回复
热议问题