繁体   English   中英

ValueError:更改值时解包的值太多(预期为 2)错误

[英]ValueError: too many values to unpack (expected 2) error when changing a value

当我将一个像1234567890这样的键传递给这个函数以在它的值上加一个时uses

def addUse(key):
    with open ("keys.json") as f:
        data = json.loads(f.read())
        for i, d in data["keys"]:
            if(d["key"] == key):
                d["uses"] = d["uses"] + 1

    with open ("keys.json", "w+") as f:
        json.dump(data,f)

我收到错误

Traceback (most recent call last):
  File "C:\Users\Jlp02\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "index.py", line 104, in redeem
    await addUse(key)
    for i, d in data["keys"]:
ValueError: too many values to unpack (expected 2)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Jlp02\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jlp02\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: too many values to unpack (expected 2)

这是keys.json 文件的样子

{
  "keys": [
    {
      "key": 92001134281235250,
      "role": "MODERATOR",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 69745445001637090,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 23658745999062060,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 23606241449357936,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 2577578387864023,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 23025062092625228,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 71812399761452820,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 43292816397532480,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 28871100160463480,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 58856807043840170,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    },
    {
      "key": 14934056480328052,
      "role": "Supporter",
      "Server": 753230650181550100,
      "uses": 0,
      "maxuses": 0
    }
  ]
}

我只需要删除i中的

for i, d in data["keys"]:
            if(d["key"] == key):
                d["uses"] = d["uses"] + 1

所以它会

for d in data["keys"]:
            if(d["key"] == key):
                d["uses"] = d["uses"] + 1

data.keys()对于任何字典,返回doc 中定义的字典中的视图。

如果您想遍历字典中的键值对,您可能想尝试这样的操作

for key, value in my_dict.items():
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM