Json从Bot编辑

时间:2020-07-15 16:03:22

标签: python discord.py

当用test命令写入所需的数字时,它将传递给json。

enter image description here

然后使用test1命令,更改number3,但整行都在更改

enter image description here

@bot.command()
    async def test(ctx, number1: str, number2: str, number3: str):
        with open('data/data.json', 'r') as f:
             data = json.load(f)
             data[str(ctx.guild.id)] = {
                 'number1': number1,
                 'number2': number2,
                 'number3': number3
             }
    
        with open('data/data.json', 'w') as f:
             json.dump(data, f, indent=4)
    
    @bot.command()
    async def test1(ctx, number1: str):
        with open('data/data.json', 'r') as f:
             data = json.load(f)
             data[str(ctx.guild.id)] = {
                 'number1': number1
             }
        with open('data/data.json', 'w',)as f:
             json.dump(data, f, indent=4)

1 个答案:

答案 0 :(得分:1)

此代码:

data[str(ctx.guild.id)] = {
    'number1': number1
}

将为{ 'number1': number1 }的{​​{1}}字段分配一个新的字典"714235...",因此原来的字典(包含data)将消失并被垃圾回收。

您是要更改单个 number1, number2, number3键吗?

"number1"
相关问题