ValueError:解包的值太多(Python 2.7)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24212686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:13:32  来源:igfitidea点击:

ValueError: too many values to unpack (Python 2.7)

pythonunpack

提问by Ionu?

values = data.split("\x00")

username, passwordHash, startRoom, originUrl, bs64encode = values
if len(passwordHash)!= 0 and len(passwordHash)!= 64:
        passwordHash = ""
if passwordHash != "":
        passwordHash = hashlib.sha512(passwordHash).hexdigest()
username = username.replace("<", "")
if len(startRoom) > 200:
        startRoom = ""
startRoom = self.roomNameStrip(startRoom, "2").replace("<","").replace("&amp;#", "&amp;amp;#")
self.login(username, passwordHash, startRoom, originUrl)  


Error:
username, passwordHash, startRoom, originUrl, bs64encode = values
ValueError: too many values to unpack

采纳答案by Martin Konecny

Check the output of

检查输出

print len(values)

It has more than 5 values (which is the number of variables you are trying to "unpack" it to) which is causing your "too many values to unpack" error:

它有 5 个以上的值(这是您尝试将其“解包”到的变量数),这会导致“解包的值太多”错误:

username, passwordHash, startRoom, originUrl, bs64encode = values

If you want to ignore the tail end elements of your list, you can do the following:

如果要忽略列表的尾端元素,可以执行以下操作:

#assuming values has a length of 6
username, passwordHash, startRoom, originUrl, bs64encode, _ = values

or unpack only the first 5 elements (thanks to @JoelCornett)

或仅解压前 5 个元素(感谢 @JoelCornett)

#get the first 5 elements from the list
username, passwordHash, startRoom, originUrl, bs64encode = values[:5]

回答by Magnun Leno

When you're doing values = data.split("\x00")it's producing more then 5 elements, probably not all values are separated by \x00.

当您这样做时,values = data.split("\x00")它会产生超过 5 个元素,可能并非所有值都由\x00.

Inspect the value of valueswith print valuesand check it's size with len(values)

检查valueswith的值print values并检查它的大小len(values)