我有大量的字典,所以我创建了新的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
}
]
}
]
请建议更好的方法
1条答案
按热度按时间kgsdhlau1#
只要在你的文件
open
子句中使用json.dump
-