擅长:python、mysql、java
<p>首先,应该使用<code>json.load</code>加载文件,然后使用indent选项is <code>json.dump</code>。你知道吗</p>
<pre><code>import json
initial = json.load('yourfile.json')
json.dump(initial, 'yourfile.json', indent = 4)
</code></pre>
<p>您还可以使用sort\u keys选项,为json文件排序键。你知道吗</p>
<pre><code>json.dump(initial, 'yourfile.json', sort_keys = True, indent = 4)
</code></pre>
<p>如果在一个大文件中有多个<em>有效的</em>json文件,则可以执行以下操作:</p>
<pre><code>with open('yourfile.json') as fp:
file = fp.read()
file = "[" + file + "]"
initial = json.loads(file)
</code></pre>
<p>只要有逗号就行。你知道吗</p>