Python 无法写入JSON文件

Python 无法写入JSON文件,python,json,python-3.x,discord.py,Python,Json,Python 3.x,Discord.py,我目前正在为我的discord机器人制作一个徽章系统,我希望有一个命令来修改用户徽章。我已经做了代码,但它没有编辑它 我的代码: @client.command() @commands.guild_only() async def modifybadges(ctx, userid, badge): try: data = get_data() for profile in data: if profile['discord_id']

我目前正在为我的discord机器人制作一个徽章系统,我希望有一个命令来修改用户徽章。我已经做了代码,但它没有编辑它

我的代码:

@client.command()
@commands.guild_only()
async def modifybadges(ctx, userid, badge):
    try:
        data = get_data()
        for profile in data:
            if profile['discord_id'] == f"{userid}":
                profile = {'discord_id': f"{userid}", 'badges': f"{badge}"}
        await ctx.send(f"Successfully gave user <@{userid}> badge {badge}.")
    except Exception as e:
        await ctx.send(f"error {e}")

如果您对如何修复有任何想法,请在下面留下答复,谢谢。

试试这个。如果不起作用,请提供您收到或发出的错误

with open('profiles.json', 'w') as file:
    json.dump(data, file)
**试试这个**
请向我们提供准确的错误

请更具体。当你说它“没有编辑它”时,你是指当你写入文件时:file.write(dumps(data,indent=2))?是的。你的答案和他的代码之间的唯一区别是缩进是4而不是2。这可能会改变JSON的格式,但不会解决写入文件的问题。
with open('profiles.json', 'w') as file:
    json.dump(data, file)
with open('profiles.json', 'w') as file:
     file.write(dumps(data, indent=4))