我如何删除 JSON 的一部分,有人可以解释它是如何工作的吗?
How can I remove a part of the JSON, and can someone explain how It works?
所以我是 JSON 文件的新手,我试图让我的 python 程序在发生某些事情时删除 JSON “行”,我得到了代码写在 JSON 但我不知道它是如何工作的,如果有人解释它是如何工作的,我将不胜感激:
with open('guilds.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
guilds_dict[str(ctx.guild.id)] = str(channel.id)
with open('guilds.json', 'w', encoding='utf-8') as f:
json.dump(guilds_dict, f, indent=4, ensure_ascii=False)
await ctx.send(f'Seleccionado el canal {channel.name} como canal de Bienvenida de {ctx.message.guild.name}')
JSON 看起来像这样:
{
"692539777958019103": "692539778494890027",
"972102908529225758": "972102909372297228"
}
如果 var guid
有 972102908529225758
我该如何删除这部分? "972102908529225758": "972102909372297228"
这就像一本字典,所以一个简单的 guilds_dict.pop('Your key') 就足够了;
How could I do to for example if the var guid has 972102908529225758 how can I delete this part? "972102908529225758": "972102909372297228"
guilds_dict.pop('972102908529225758');
所以我是 JSON 文件的新手,我试图让我的 python 程序在发生某些事情时删除 JSON “行”,我得到了代码写在 JSON 但我不知道它是如何工作的,如果有人解释它是如何工作的,我将不胜感激:
with open('guilds.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
guilds_dict[str(ctx.guild.id)] = str(channel.id)
with open('guilds.json', 'w', encoding='utf-8') as f:
json.dump(guilds_dict, f, indent=4, ensure_ascii=False)
await ctx.send(f'Seleccionado el canal {channel.name} como canal de Bienvenida de {ctx.message.guild.name}')
JSON 看起来像这样:
{
"692539777958019103": "692539778494890027",
"972102908529225758": "972102909372297228"
}
如果 var guid
有 972102908529225758
我该如何删除这部分? "972102908529225758": "972102909372297228"
这就像一本字典,所以一个简单的 guilds_dict.pop('Your key') 就足够了;
How could I do to for example if the var guid has 972102908529225758 how can I delete this part? "972102908529225758": "972102909372297228"
guilds_dict.pop('972102908529225758');