JSON 文件无法正确加载
JSON file won't load properly
amount = {}
with open('amount.json', 'r') as toload:
amount = json.load(toload)
这应该加载我的 .json
文件,但它没有加载 - 我没有收到任何错误,我的字典只是没有更新。我已经检查了 .json
文件并且我的条目在那里,但是当我检查我的余额时它说我没有帐户。这是平衡命令:
@client.command(name='balance',
aliases=['bal'],
pass_ctx=True)
async def balance(ctx):
id = ctx.message.author.id
if id not in amount:
await ctx.send('You are not registered. Register with `k!reg`!')
return
await ctx.send('You have {} doughnuts'.format(amount[id]))
出于某种原因,它认为我的 ID 不在字典中。我是否加载了 .json
文件错误?抱歉,如果这是一个明显的问题,我似乎找不到原因。
正如凯文所说,我的 id
变量是一个整数,而在我的字典中它是一个字符串。将 id
设为字符串后,成功了!
感谢大家的宝贵意见!
amount = {}
with open('amount.json', 'r') as toload:
amount = json.load(toload)
这应该加载我的 .json
文件,但它没有加载 - 我没有收到任何错误,我的字典只是没有更新。我已经检查了 .json
文件并且我的条目在那里,但是当我检查我的余额时它说我没有帐户。这是平衡命令:
@client.command(name='balance',
aliases=['bal'],
pass_ctx=True)
async def balance(ctx):
id = ctx.message.author.id
if id not in amount:
await ctx.send('You are not registered. Register with `k!reg`!')
return
await ctx.send('You have {} doughnuts'.format(amount[id]))
出于某种原因,它认为我的 ID 不在字典中。我是否加载了 .json
文件错误?抱歉,如果这是一个明显的问题,我似乎找不到原因。
正如凯文所说,我的 id
变量是一个整数,而在我的字典中它是一个字符串。将 id
设为字符串后,成功了!
感谢大家的宝贵意见!