写入文件时如何保留json文件格式

问题描述 投票:0回答:1

我有两个json文件data1.json和data2.json。我比较两个文件并将 data1.json 中发现的差异输出到新文件 data3.json。我想将 data3.json 的格式保留为 data1.json 或 data2.json。

数据1.json:

    [
        {
            "userid": "1290126777",
            "status": "UserStatus.RECENTLY",
            "name": "Apple Plus",
            "bot": false,
            "username": "None"
        },
        {
            "userid": "1005441066",
            "status": "UserStatus.RECENTLY",
            "name": "SNGOUN",
            "bot": false,
            "username": "Dr_PHEAKDEY_Abdominal"
        },
        {
            "userid": "6590307444",
            "status": "UserStatus.RECENTLY",
            "name": "Amisha",
            "bot": false,
            "username": "Amishawenda"
        },
        {
            "userid": "5474490111",
            "status": "UserStatus.RECENTLY",
            "name": "Ah Nin",
            "bot": false,
            "username": "None"
        }
    ]


    data2.json:

    [
        {
            "userid": "1290126990",
            "status": "UserStatus.RECENTLY",
            "name": "Apple Plus",
            "bot": false,
            "username": "None"
        },
        {
            "userid": "1005441066",
            "status": "UserStatus.RECENTLY",
            "name": "SNGOUN",
            "bot": false,
            "username": "Dr_PHEAKDEY_Abdominal"
        },
        {
            "userid": "6590307468",
            "status": "UserStatus.RECENTLY",
            "name": "Amisha",
            "bot": false,
            "username": "Amishawenda"
        },
        {
            "userid": "5474490329",
            "status": "UserStatus.RECENTLY",
            "name": "Ah Nin",
            "bot": false,
            "username": "None"
        }
    ]


    Here are my codes:

    import json
    with open("data1.json", "r", encoding='utf-8') as f1:
        data1 = json.loads(f1.read())
    with open("data2.json", "r", encoding='utf-8') as f2:
        data2 = json.loads(f2.read())

    with open('data3.json', 'w', encoding='utf-8') as nf:
      nf.write('[' + '\n')
      for item in data1:
        if item['userid'] not in [x['userid'] for x in data2]:
          json.dump(item, nf, ensure_ascii=False, indent=4)
          nf.write(',' + '\n')
      nf.write(']')
      

    Here are the results in data3.json:

    [
    {
        "userid": "1290126777",
        "status": "UserStatus.RECENTLY",
        "name": "Apple Plus",
        "bot": false,
        "username": "None"
    },
    {
        "userid": "6590307444",
        "status": "UserStatus.RECENTLY",
        "name": "Amisha",
        "bot": false,
        "username": "Amishawenda"
    },
    {
        "userid": "5474490111",
        "status": "UserStatus.RECENTLY",
        "name": "Ah Nin",
        "bot": false,
        "username": "None"
    },
    ]   

我希望 data3.json 与 data1.json 或 data2.json 格式相同,应该是:

-开头有“[”,结尾有“]”

-每项前面的缩进

-最后一项没有“,”

    The format of data3.json should be like:

    [
            {
                "userid": "1290126777",
                "status": "UserStatus.RECENTLY",
                "name": "Apple Plus",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "6590307444",
                "status": "UserStatus.RECENTLY",
                "name": "Amisha",
                "bot": false,
                "username": "Amishawenda"
            },
            {
                "userid": "5474490111",
                "status": "UserStatus.RECENTLY",
                "name": "Ah Nin",
                "bot": false,
                "username": "None"
            }
    ]

有什么办法可以实现上述目的吗?

谢谢

json format indentation
1个回答
0
投票

最好构建所需项目的列表并将其转储为 json 格式的文件。 比如:

import json
    
with open("data1.json", "r", encoding='utf-8') as f1:
    data1 = json.load(f1)
with open("data2.json", "r", encoding='utf-8') as f2:
    data2 = json.load(f2)
        
data3 = []
for item in data1:
    print(item)
    if item['userid'] not in [x['userid'] for x in data2]:
        data3.append(item)
print(d3)
        
with open('data3.json', 'w', encoding='utf-8') as nf:
    json.dump(data3, nf, indent=4)
© www.soinside.com 2019 - 2024. All rights reserved.