有没有一种方法可以将条目添加到json列表中的字典中?

时间:2020-09-11 06:18:26

标签: python json discord discord.py

是否可以将条目添加到json列表中的字典中? (P.S.我什至不确定我是否应该这样说这个问题)


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"
        }
    }
}

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

看起来像这样

{
 "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"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

首先将字典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'
#             }
#         }
#     ]
# }