无法正确使用 python 写入 JSON 文件
Not able to write to JSON files using python correctly
所以我有一个 discord 机器人,它保存了 minecraft 中一些地方的坐标。但是在保存坐标时,我无法正确写入 JSON 文件。当我第一次保存时它写得很好但是当我 运行 它再次给我错误。
代码:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name,*, cords):
uploader = ctx.author
data = {"Uploader":f"{uploader}","Cords":f"{cords}","Name":f"{name}"}
with open(f"test.json","a") as f:
json.dump(data, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")
JSON 一次写入后的文件:
{
"Uploader": "SastaSushi#6366",
"Cords": "-88 0 -110",
"Name": "cords"
}
JSON文件再次写入时:
{
"Uploader": "SastaSushi#6366",
"Cords": "-88 0 -110",
"Name": "cords"
}{
"Uploader": "SastaSushi#6366",
"Cords": "-70 0 900",
"Name": "village"
}
第二次写入时的错误如下:
我想确保它以没有错误的方式写入,并且我可以在需要时读取数据。
所以,你写的方式最终是错误的正确的方式是:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name,*, cords):
uploader = ctx.author
with open("test.json",r) as file:
data = json.load(file)
nd = {}
nd["uploader"] = uploader
nd["Cords"] = cords
nd["Name"] = name
with open(f"test.json","a") as f:
json.dump(nd, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")
尝试这个如果它不起作用回复我错误
所以,有人发布了此问题的答案但已将其删除,而且有效!
所以这是现在的代码:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name, *, cords):
uploader = ctx.author
try:
with open("Data\file.json") as f:
data = json.load(f)
except FileNotFoundError:
data = []
if isinstance(data, dict):
data = [data]
data.append({"Uploader": f"{uploader}", "Cords": f"{cords}", "Name": f"{name}"})
with open("Data\file.json","w") as f:
json.dump(data, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")```
所以我有一个 discord 机器人,它保存了 minecraft 中一些地方的坐标。但是在保存坐标时,我无法正确写入 JSON 文件。当我第一次保存时它写得很好但是当我 运行 它再次给我错误。
代码:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name,*, cords):
uploader = ctx.author
data = {"Uploader":f"{uploader}","Cords":f"{cords}","Name":f"{name}"}
with open(f"test.json","a") as f:
json.dump(data, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")
JSON 一次写入后的文件:
{
"Uploader": "SastaSushi#6366",
"Cords": "-88 0 -110",
"Name": "cords"
}
JSON文件再次写入时:
{
"Uploader": "SastaSushi#6366",
"Cords": "-88 0 -110",
"Name": "cords"
}{
"Uploader": "SastaSushi#6366",
"Cords": "-70 0 900",
"Name": "village"
}
第二次写入时的错误如下:
我想确保它以没有错误的方式写入,并且我可以在需要时读取数据。
所以,你写的方式最终是错误的正确的方式是:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name,*, cords):
uploader = ctx.author
with open("test.json",r) as file:
data = json.load(file)
nd = {}
nd["uploader"] = uploader
nd["Cords"] = cords
nd["Name"] = name
with open(f"test.json","a") as f:
json.dump(nd, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")
尝试这个如果它不起作用回复我错误
所以,有人发布了此问题的答案但已将其删除,而且有效! 所以这是现在的代码:
@client.command(aliases = ["sc"])
async def save_cords(ctx, name, *, cords):
uploader = ctx.author
try:
with open("Data\file.json") as f:
data = json.load(f)
except FileNotFoundError:
data = []
if isinstance(data, dict):
data = [data]
data.append({"Uploader": f"{uploader}", "Cords": f"{cords}", "Name": f"{name}"})
with open("Data\file.json","w") as f:
json.dump(data, f, indent=4)
await ctx.send(f"Successfully saved cords. | Name: {name} | Cords: {cords}")```