Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

ValueError: too many values to unpack (Python 2.7)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...