Read JSON file and Pretty-Print it to another file

后端 未结 2 521
梦毁少年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 07:47

    Yes, you can use open() to read/write file, then json.load() to read the file and json.dump(..., indent=4) to pretty-print it.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题