python-3.x 一些字典如何创建新的JSON文件在一个列表和删除最后一个逗号从JSON文件

qeeaahzv  于 2023-01-22  发布在  Python
关注(0)|答案(1)|浏览(161)

我有大量的字典,所以我创建了新的Json文件与字典,但字典不添加到一个列表,而创建新的Json文件,它不会删除最后一个逗号
在这里我写我的数据看起来像,谁能建议更好的方式
提前感谢帮助将不胜感激!

json_dict = {}
for li in jsonli:
  json_dict.update(li)

with open(r"D:\Users\laki.aya\Desktop\Invalidemail\stc.json", 'a+') as fout:
  fout.write(json.dumps(json_dict, indent = 4,default=str))
  fout.write(',\n')
  fout.close()

我得到的输出如下所示:

{
    "email": "zxxxccxxcc@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 5123456
        },
        {
            "property": "register_time",
            "value": "2022-12-01"
        },
        {
            "property": "linked_alexa",
            "value": 0
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
},
{
    "email": "zyyyyyyuser@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 188882272
        },
        {
            "property": "register_time",
            "value": "2022-09-16"
        },
        {
            "property": "linked_alexa",
            "value": 1
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
},
{
    "email": "z3@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 100000708
        },
        {
            "property": "register_time",
            "value": "2022-11-30"
        },
        {
            "property": "linked_alexa",
            "value": 0
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
},

我希望数据应该在一个列表中,并删除最后一个逗号
预期

[  
    {
    "email": "zxxxccxxcc@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 5123456
        },
        {
            "property": "register_time",
            "value": "2022-12-01"
        },
        {
            "property": "linked_alexa",
            "value": 0
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
},
{
    "email": "zyyyyyyuser@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 188882272
        },
        {
            "property": "register_time",
            "value": "2022-09-16"
        },
        {
            "property": "linked_alexa",
            "value": 1
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
},
{
    "email": "z3@gmail.com",
    "properties": [
        {
            "property": "XlinkUserID",
            "value": 100000708
        },
        {
            "property": "register_time",
            "value": "2022-11-30"
        },
        {
            "property": "linked_alexa",
            "value": 0
        },
        {
            "property": "linked_googlehome",
            "value": 0
        },
        {
            "property": "n0x89_direct_connect_full_color_bulb__a19_",
            "value": 1
        }
    ]
}
]

请建议更好的方法

kgsdhlau

kgsdhlau1#

只要在你的文件open子句中使用json.dump-

import json

# some example data
json_data = [{'name':'Joe', 'age':40}, {'name':'Jane', 'age':50}]

with open('json_file_out.json', 'w') as fout:
    json.dump(json_data, fout)

相关问题