【发布时间】:2022-01-07 09:57:53
【问题描述】:
我正在尝试将字典存储在用户输入的 JSON 文件中。代码是:
@client.command()
async def shibaku0(ctx, coin1, coin2, coin3, coin4, coin5, coin6, shibakunumber, oslink):
await ctx.message.delete()
with open('Shibaku0.json', 'r') as f:
coins_data = json.load(f)
coins_data[str(ctx.author.id)]["coins"] = (coin1, coin2, coin3, coin4, coin5, coin6)
shibakunumber[str(ctx.author.id)]["shibakunumber"] = (shibakunumber)
oslink[str(ctx.author.id)]["oslink"] = (oslink)
with open('Shibaku0.json', 'w') as f:
json.dump(coins_data, f)
embed=discord.Embed(title="Shibaku0", url=f'{oslink}', description=f'No. {shibakunumber}')
embed.add_field(name="Coins: ", value=f'{coin1} {coin2} {coin3} {coin4} {coin5} {coin6}', inline=True)
embed.set_footer(text=f"{ctx.author.name}'s Shibaku")
await ctx.send(embed=embed)
我想存储“coins”、“shibakunumber”、“oslink”,但是当我尝试运行代码时收到此错误消息:
TypeError: 列表索引必须是整数或切片,而不是 str
【问题讨论】:
-
一个有效的 JSON 文件通常以
{或[开头。在{的情况下,这是一个类似于python 中的dict的对象。以[开头表示它是一个类似python中list的数组。 -
那么我需要更改哪个支架才能使其工作? @MYousefi
-
您的数据应该是什么样的?它是单个实体还是实体列表,每个实体都有自己的 id 和硬币?
-
基本上我想看起来像:" userid: { coin: "value", shibakunumber: "value" oslink: "link format" " 这是因为我希望能够单独调用每个项目@MYousefi
标签: discord.py