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缩进/漂亮打印到新的文本文件中?
2 个解决方案
#1
3
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)
#2
1
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.
是的,您可以使用open()来读/写文件,然后使用json.load()来读取文件,使用json.dump(...,indent = 4)来打印它。
#1
3
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)
#2
1
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.
是的,您可以使用open()来读/写文件,然后使用json.load()来读取文件,使用json.dump(...,indent = 4)来打印它。