[英]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.