如何在python中重新格式化json

时间:2017-04-01 08:56:33

标签: python arrays json python-3.x

我正在尝试创建一个json文件,因为我是python中的新手 json文件 我有如下,我想重新格式化

{  
   "A1":"a1",
   "aback":"\u0259b\u00e6k",
   "abaft":"abaft",
   "abandon":"\u0259b\u00e6nd\u0259n",
   "abandoned":"\u0259b\u00e6nd\u0259nd",
   "abandonment":"\u0259b\u00e6nd\u0259nm\u0259nt",
   "abase":"abase",
   "abash":"\u0259b\u00e6\u0283",
   "abashment":"abashment",
   "abate":"\u0259bet",
   "abatement":"\u0259betm\u0259nt",
   "abbey":"\u00e6bi",
   "abbreviate":"\u0259briviet",
   "abbreviation":"\u0259brivie\u0283\u0259n"
}

我想要实现的格式是

{  
   word: "A1",
   transcription:"a1"
}
{
   word: "aback",
   transcription :"\u0259b\u00e6k"
}
{
  word:"abaft"
  transcrition:"abaft"
}
{
  word:"abbreviation",
  transcription:"\u0259brivie\u0283\u0259n"
}

3 个答案:

答案 0 :(得分:2)

从文件中读取JSON并将其转换为字典。使用列表推导生成字典列表并将其写为JSON列表:

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)

对于您的输入文件,这将生成一个包含以下内容的输出文件:

[{"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"}]

您可以稍微格式化输出,使用indent

json.dump([{'word': k, 'transcription': d[k]} for k in d], outfile, indent='')

将输出:

[
    {
        "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"
    }
]

请注意,您请求的格式实际上不是有效的JSON。如果您不想使用逗号和列表括号,可以像这样编写文件:

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')
{
    "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"
}

最后,如果订单很重要,您可以遍历排序的密钥,只需使用:

for k in sorted(d)

在适当的地方。

答案 1 :(得分:0)

In [16]: d
Out[16]: {'a': 'A', 'b': 'B', 'c': 'C'}

In [17]: lis = []

In [18]: lis
Out[18]: []

In [19]: for key in d:
    ...:     dd ={}
    ...:     dd["word"] = key
    ...:     dd["transcrition"] = d[key]
    ...:     lis.append(dd)
    ...:     

In [20]: lis
Out[20]: 
[{'transcrition': 'A', 'word': 'a'},
 {'transcrition': 'C', 'word': 'c'},
 {'transcrition': 'B', 'word': 'b'}]

答案 2 :(得分:0)

将此代码复制到convert.py

等文件中
import sys
import json
from pprint import PrettyPrinter, pprint


def main():
    fn =   sys.argv[1]
    with open(fn, 'rb') as f:
        data = json.loads(f.read())

    for k, v in data.items():
        pprint({'word':k, 'transcription':v})


if __name__ == '__main__':
    main()

然后运行命令python convert.py YOUR_JSON_FILE_NAME,享受它〜