Read JSON file and Pretty-Print it to another file

后端 未结 2 526
梦毁少年i
梦毁少年i 2021-01-27 07:30

I have a complex (nested) json text file that is one long line in the text file

Is there any way to read in the file (in python) and indent / pretty-print the json to a

2条回答
  •  佛祖请我去吃肉
    2021-01-27 08:11

    Read the file using json.load(..), and use json.dump(..) to write the object out to another file while specifying indent value.

    with open("inFile") as f, open("outFile", "w") as g:
        json.dump(json.load(f), g, indent=4)
    

提交回复
热议问题