Discord.py |没有足够的值来解包


with open("warns.json", "r") as f:
data = json.load(f)
user_data = data[str(a)]
await ctx.send(f"Total warnings: {len(user_data)}")
for mod , reason, time, warn_id, warns in user_data:
warn_id_ = warn_id
mod_ = mod
reason_ = reason
time_ = time
warns_ = warns
await ctx.send(f"ID: {warn_id_}, mod: {mod_}, reason: {reason_}, time: {time_}, warns: {warns_}")´´´

This is My Code..我尝试从我的warnings .json中获取以下值!

warn_id mod reason time warnings

这是我的Json:
{
"836495228629417984": {
"mod": [
763339711988236328
],
"reason": [
"test"
],
"time": [
"Friday, October 08 2021 @ 23:15:31 PM"
],
"warn_id": [
83299
],
"warns": 1
}

但是它返回:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: not enough values to unpack (expected 5, got 3)

I don ' t Know how to fix that:/

首先,您的示例json是不正确的,它最后缺少一个}。现在你的解包程序可能像这样:

with open("warns.json", "r") as f:
data = json.load(f)
for k in data:
unpacked = data[k]
warn_id_ = unpacked['warn_id']
mod_ = unpacked['mod']
reason_ = unpacked['reason']
time_ = unpacked['time']
warns_ = unpacked['warns']

注意-这个最小的改变只是为了使你的程序工作。但也许还有其他更好的解决办法。

最新更新