简体   繁体   中英

Read JSON file and Pretty-Print it to another file

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 new text file?

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)

是的,您可以使用open()读取/写入文件,然后使用json.load()读取文件,然后json.load() json.dump(..., indent=4)进行漂亮打印。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM