Как сделать запись в json файл с python?

Делаю бота экономики на discord.py, использую json для хранения 2х типов валют. Код записи:

@bot.command()
async def test(ctx):
    with open('economy.json','r') as f:
        money = json.load(f)
    with open('economy.json','w') as f:
        json.dump(money,f)
    guild = ctx.guild.id
    author = ctx.author.id
    money[str(guild)] = {}
    money[str(guild)][author] = {}
    money[str(guild)][str(author)] = 0
    money[str(ctx.author.id)]['Bitcoin'] = 0

Текст ошибки:

Ignoring exception in command test:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "d:\pyt\bot\chillzone\chillzonebot.py", line 52, in test
    money[str(ctx.author.id)]['Bitcoin'] = 0
KeyError: '427047904209928193'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: '427047904209928193'

Код ошибки '427047904209928193' это айди пользователя.


Ответы (0 шт):