有没有办法将条目添加到json列表中的字典中?

2024-09-22 16:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法将条目添加到json列表中的字典中?(顺便说一句,我甚至不确定我应该如何表达这个问题)


base_server_datas = {
    'users': {},
}

    @commands.command()
    @commands.has_any_role(*MODERATORS, *ADMINISTRATORS)
    async def create(self, ctx, *args: str):
        faction = load_faction()
        message = " ".join(args).lower()
        new_faction = message
        if new_faction not in faction:
            faction[new_faction] = base_server_datas.copy()

            save_faction(faction)
            await ctx.send(f'**{new_faction}** cult created!')
        else:
            await ctx.send(f"{new_faction} cult already exists")

    @commands.command(case_insensitive=True)
    async def join(self, ctx, *args: str):
        faction = load_faction()
        new_user = str(ctx.author.id)
        message = " ".join(args).lower()
        existing_faction = message
        if existing_faction in faction:
            if new_user not in faction[existing_faction]["users"]:
                faction[existing_faction]["users"][new_user] = ctx.author.name
                save_faction(faction)
                await ctx.send(f"{ctx.author.mention} joined the **{message}** cult")
            else:
                await ctx.send(f"{ctx.author.mention} is already in the cult")
        else:
            await ctx.send("faction doesn\'t exist")

因此,使用这段代码,我可以创建一个派系,也可以加入它,它看起来像这样:

{
    "h": {
        "users": {
            "535485026905882626": "Benji",
            "702646374155419648": "rosesaredumb",
            "578938036960624660": "invalid-user",
            "360366991149891585": "Goodra999"
        }
    }
}

但我做了另一个标题“领导者”,并尝试在其下添加条目,但不起作用

看起来像这样

{
 "h": [
        {
            "users": {}
        },
        {
            "leaders": {}
        }
    ]
}

这部分的代码是



base_server_datas2 = {
    'leaders': {},
}

@commands.command()
    async def leader(self,ctx, member: discord.Member, *args: str):
        faction = load_faction()
        new_user = str(member.id)
        message = " ".join(args).lower()
        existing_faction = message
        if existing_faction in faction:
            if new_user not in faction[existing_faction]["leaders"]:
                faction[existing_faction]["leaders"][new_user] = member.name
                save_faction(faction)
                await ctx.send(f"{ctx.author.mention} joined the **{message}** cult")
            else:
                await ctx.send(f"{ctx.author.mention} is already in the cult")
        else:
            await ctx.send("faction doesn\'t exist")

在添加“领导者”标题时,现在我甚至无法更新“用户”字典

有什么办法可以把词条都加到这两本字典里吗

编辑:这是我想要的输出

{
 "h": [
        {
            "users": {
             "702646374155419648": "rosesaredumb",
             "578938036960624660": "invalid-user"
            }
        },
        {
            "leaders": {
                        "57893803696076560": "josh"
            }
        }
    ]
}

Tags: insendmessagenewifargsawaitusers
1条回答
网友
1楼 · 发布于 2024-09-22 16:42:07

首先将字典d["h"]转换为包含当前值的列表,并将所需的领导人字典添加到列表中:

di = {
    "h": {
        "users": {
            "535485026905882626": "Benji",
            "702646374155419648": "rosesaredumb",
            "578938036960624660": "invalid-user",
            "360366991149891585": "Goodra999"
        }
    }
}

to_add = {
    "leaders": {
        "57893803696076560": "josh"
    }
}

di["h"] = [di["h"], to_add]

print(di)

# Output:
# {
#     'h': [
#         {
#             'users': {
#                 '535485026905882626': 'Benji',
#                 '702646374155419648': 'rosesaredumb',
#                 '578938036960624660': 'invalid-user',
#                 '360366991149891585': 'Goodra999'
#             }
#         },
#         {
#             'leaders': {
#                 '57893803696076560': 'josh'
#             }
#         }
#     ]
# }

相关问题 更多 >