简体   繁体   English

读取JSON文件并将其漂亮打印到另一个文件

[英]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 我有一个复杂的(嵌套的)json文本文件,该文本文件中的一行很长

Is there any way to read in the file (in python) and indent / pretty-print the json to a new text file? 有什么方法可以读取文件(在python中)并将json缩进/漂亮打印到新的文本文件中?

Read the file using json.load(..) , and use json.dump(..) to write the object out to another file while specifying indent value. 使用json.load(..)读取文件,并使用json.dump(..)将对象写到另一个文件中,同时指定缩进值。

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)进行漂亮打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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