读取JSON文件并将其漂亮打印到另一个文件 [英] Read JSON file and Pretty-Print it to another file
本文介绍了读取JSON文件并将其漂亮打印到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个复杂的(嵌套的)json文本文件,该文本文件中的一行很长
I have a complex (nested) json text file that is one long line in the text file
有什么方法可以读取文件(在python中)并将json缩进/漂亮地打印到新的文本文件中?
Is there any way to read in the file (in python) and indent / pretty-print the json to a new text file?
推荐答案
使用json.load(..)
读取文件,并使用json.dump(..)
将对象写出到另一个文件中,同时指定缩进值.
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)
这篇关于读取JSON文件并将其漂亮打印到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文