How to prettyprint a JSON file?

前端 未结 13 994
滥情空心
滥情空心 2020-11-22 01:53

I have a JSON file that is a mess that I want to prettyprint. What\'s the easiest way to do this in Python?

I know PrettyPrint takes an \"object\", which I think can

13条回答
  •  不知归路
    2020-11-22 02:25

    You could use the built-in module pprint (https://docs.python.org/3.6/library/pprint.html).

    How you can read the file with json data and print it out.

    import json
    import pprint
    
    json_data = None
    with open('file_name.txt', 'r') as f:
        data = f.read()
        json_data = json.loads(data)
    
    pprint.pprint(json_data)
    

提交回复
热议问题