<p>从文件中读取JSON并将其转换为字典。使用列表理解生成字典列表,并将其写成JSON列表:</p>
<pre><code>import json
with open('file.json') as infile, open('out.json', 'w') as outfile:
d = json.load(infile)
json.dump([{'word': k, 'transcription': d[k]} for k in d], outfile)
</code></pre>
<p>对于输入文件,这将生成一个输出文件,其中包含:</p>
<pre>
[{"transcription": "\u0259b\u00e6\u0283", "word": "abash"}, {"transcription": "\u0259briviet", "word": "abbreviate"}, {"transcription": "abaft", "word": "abaft"}, {"transcription": "a1", "word": "A1"}, {"transcription": "\u0259b\u00e6nd\u0259nd", "word": "abandoned"}, {"transcription": "\u0259b\u00e6nd\u0259nm\u0259nt", "word": "abandonment"}, {"transcription": "\u0259betm\u0259nt", "word": "abatement"}, {"transcription": "\u0259bet", "word": "abate"}, {"transcription": "\u0259b\u00e6nd\u0259n", "word": "abandon"}, {"transcription": "\u00e6bi", "word": "abbey"}, {"transcription": "\u0259brivie\u0283\u0259n", "word": "abbreviation"}, {"transcription": "\u0259b\u00e6k", "word": "aback"}, {"transcription": "abase", "word": "abase"}, {"transcription": "abashment", "word": "abashment"}]
</pre>
<p>您可以稍微格式化输出,使用<code>indent</code>:</p>
<pre><code>json.dump([{'word': k, 'transcription': d[k]} for k in d], outfile, indent='')
</code></pre>
<p>将输出:</p>
<pre>
[
{
"word": "aback",
"transcription": "\u0259b\u00e6k"
},
{
"word": "abandonment",
"transcription": "\u0259b\u00e6nd\u0259nm\u0259nt"
},
{
"word": "abatement",
"transcription": "\u0259betm\u0259nt"
},
{
"word": "abbey",
"transcription": "\u00e6bi"
},
{
"word": "abbreviation",
"transcription": "\u0259brivie\u0283\u0259n"
},
{
"word": "abandoned",
"transcription": "\u0259b\u00e6nd\u0259nd"
},
{
"word": "abash",
"transcription": "\u0259b\u00e6\u0283"
},
{
"word": "abaft",
"transcription": "abaft"
},
{
"word": "abashment",
"transcription": "abashment"
},
{
"word": "abate",
"transcription": "\u0259bet"
},
{
"word": "abbreviate",
"transcription": "\u0259briviet"
},
{
"word": "A1",
"transcription": "a1"
},
{
"word": "abandon",
"transcription": "\u0259b\u00e6nd\u0259n"
},
{
"word": "abase",
"transcription": "abase"
}
]
</pre>
<p>请注意,您请求的格式实际上不是有效的JSON。如果不需要逗号和列表括号,可以这样写出文件:</p>
<pre><code>with open('file.json') as infile, open('out.json', 'w') as outfile:
d = json.load(infile)
print(*[json.dumps({'word': k, 'transcription': d[k]}, indent=' ') for k in d], file=outfile, sep='\n')
</code></pre>
<pre>
{
"word": "aback",
"transcription": "\u0259b\u00e6k"
}
{
"word": "abandonment",
"transcription": "\u0259b\u00e6nd\u0259nm\u0259nt"
}
{
"word": "abatement",
"transcription": "\u0259betm\u0259nt"
}
{
"word": "abbey",
"transcription": "\u00e6bi"
}
{
"word": "abbreviation",
"transcription": "\u0259brivie\u0283\u0259n"
}
{
"word": "abandoned",
"transcription": "\u0259b\u00e6nd\u0259nd"
}
{
"word": "abash",
"transcription": "\u0259b\u00e6\u0283"
}
{
"word": "abaft",
"transcription": "abaft"
}
{
"word": "abashment",
"transcription": "abashment"
}
{
"word": "abate",
"transcription": "\u0259bet"
}
{
"word": "abbreviate",
"transcription": "\u0259briviet"
}
{
"word": "A1",
"transcription": "a1"
}
{
"word": "abandon",
"transcription": "\u0259b\u00e6nd\u0259n"
}
{
"word": "abase",
"transcription": "abase"
}
</pre>
<p>最后,如果顺序很重要,您可以迭代排序的键,只需使用:</p>
<p><code>for k in sorted(d)</code></p>
<p>在适当的情况下。你知道吗</p>