【问题标题】:Discord.py | not enough values to unpack不和谐.py |没有足够的值来解包
【发布时间】:2021-11-28 18:48:57
【问题描述】:
 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_}")´´´

这是我的代码。我尝试从 warns.json 中获取以下值!

warn_id mod 原因时间警告

这是我的 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)

我不知道如何解决这个问题:/

【问题讨论】:

  • 当你想要 5 样东西时,你会得到 3 样东西。要调试,您可以将 for 循环替换为 for data in user_data: print(repr(data))
  • 我只得到键名而不是键内的值:/

标签: python json discord


【解决方案1】:

首先,您的示例 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']

注意——这个最小的改变只是为了让你的程序正常工作。但可能还有其他更好的解决方案。

【讨论】:

  • 我知道,但是缺少“}”是 stackoverflow 错误,我现在要试试这个!
  • 谢谢!成功了:)你救了我哈哈
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多