Python ValueError:更改值时出现太多无法解压缩的值(预期为2)错误
当我将一个键(如Python ValueError:更改值时出现太多无法解压缩的值(预期为2)错误,python,json,Python,Json,当我将一个键(如1234567890传递到此函数)以向其值中添加一个时使用 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
1234567890
传递到此函数)以向其值中添加一个时使用
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
}
]
}
data.keys()
For any dictionary返回字典中定义的键的视图
如果要在字典中的键值对上进行迭代,可能需要尝试类似的操作
for key, value in my_dict.items():
...
我只需删除中的
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
用于数据[“键”]中的i,d
为什么<代码>数据[“键”]是一个列表
请用完整的错误回溯更新您的问题。my_dict.keys()
?是不是items()
?哈哈,谢谢@deadshot,看起来我5分钟都无法集中注意力:p