Discuss ValueError: Need more than 1 value to unpack. in the Python Programming forum on Dev Shed. ValueError: Need more than 1 value to unpack. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 61
Time spent in forums: 1 Day 1 h 6 m 11 sec
Reputation Power: 1
ValueError: Need more than 1 value to unpack.
ValueError: Need more than 1 value to unpack.
So this is my error, I'm following learn python the hard way exercise 13. Made this thread on Beginners but it was taking a while for a reply.
Anyway, what I had to do was type "python ex13.py first 2nd 3rd"into the python shell i think to run the program, but I get the error 'ex13 syntax error'. That's my first problem...(I've never run .py from the python shell before, usually just press f5)
So I did some searching for solutions and I came across "execfile("ex13.py")", tried that but I get ValueError: Need more than 1 value to unpack.
I tried it again by typing import sys, sys.argv with the arguments, followed by execfile but I get the same error but now it's saying 3 value's.
There was another solution on the same thread, which I haven't tried yet because it looks strange and I'm not sure if it's an optimal solution. (I don't know if it would cause problems down the track in larger programs) it is this function:
import sys
from sys import argv
sys.argv = ['first', '2nd', 'here']
script, first, second, third = argv
print "The script is called: ", script
print "Your first variable is: ", first
print "Your second variable is: ", second
print "Your third variable is ", third
Posts: 285
Time spent in forums: 4 Days 4 h 28 m 58 sec
Reputation Power: 16
Quote:
Originally Posted by breadbox
Anyway, what I had to do was type "python ex13.py first 2nd 3rd"into the python shell
Into the PYTHON shell?! That should be typed in the OS’s command line shell.
Quote:
So I did some searching for solutions and I came across "execfile("ex13.py")", tried that but I get ValueError: Need more than 1 value to unpack.
execfile() takes only one obligatory argument, so the syntax error results from something inside ex13.py.
Quote:
Code:
sys.argv = ['first', '2nd', 'here']
Why are you assigning to sys.argv? While not illegal, that’s usually not what you want to do, since sys.argv holds the command line given to your Python script.
If you want to learn about multiple assignment try
Code:
>>> a,b = 1,2
>>> a
1
>>> b
2
>>> a,b = [1,2,3] # this is the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
But mercy me! Why couple that with trying to understand command line arguments at the same time? Back up man.
Posts: 61
Time spent in forums: 1 Day 1 h 6 m 11 sec
Reputation Power: 1
Thanks for the reply guys.
So is this maybe too advanced for me? I was just following the book, and ran into the problems...I tried using those functions because it's all I could find online.
Can't I run it from the python command line? I type 'python ex13.py first 2nd 3rd' but I still get syntax error for the script. I removed sys.argv, I don't think I have any syntax errors in the file...
I still need to figure out how to use the OS command line so if that's the issue let me know. I'm moving tomorrow and have to organize so I'll try and understand this more then. Thanks for any advice/solutions you can provide until then...
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
Try this program
This program will run the same in python2 and in python3. It also works and demonstrates exercise 13 in a failsafe manner.
Open a command window, and try the commands super oscar indicated (assuming python is where you say)
D:/Python24/Python.exe Skripta.py
D:/Python24/Python.exe Skripta.py FirstArgument
D:/Python24/Python.exe Skripta.py 1st 2nd
Code:
import sys
print('sys.argv is '+str(sys.argv))
print('')
print('')
(a,b,) = (sys.argv+['python command line had no arguments'])[:2]
print('program name: '+a)
print('the argument (or not): '+b)
input('press the enter button on your keyboard to continue')
Posts: 61
Time spent in forums: 1 Day 1 h 6 m 11 sec
Reputation Power: 1
Quote:
Originally Posted by b49P23TIvg
This program will run the same in python2 and in python3. It also works and demonstrates exercise 13 in a failsafe manner.
Open a command window, and try the commands super oscar indicated (assuming python is where you say)
D:/Python24/Python.exe Skripta.py
D:/Python24/Python.exe Skripta.py FirstArgument
D:/Python24/Python.exe Skripta.py 1st 2nd
Code:
import sys
print('sys.argv is '+str(sys.argv))
print('')
print('')
(a,b,) = (sys.argv+['python command line had no arguments'])[:2]
print('program name: '+a)
print('the argument (or not): '+b)
input('press the enter button on your keyboard to continue')
Tried it, and it works! Thank you. Now I just need to try and understand it.
With this line
Code:
(a,b,) = (sys.argv+['python command line had no arguments'])[:2]
What does [:2] do? I don't think I've used it before.
Thank you for the help.
Edit: Does [:2] show the string when sys.argv has no values somehow?
Edit2: Something else that's bugging me is I can't open a script from cmd by typing "C:\Python27\python.exe Skripta.py" (that's correct directory)...I have to type C:\Python27\python to run python and then execfile("C:\Python27\Skripta.py")
Which isn't great because I can't type in arguments.
I entered the arguments in cmd using sys.argv = [] which worked, but every time after that I get a 'name 'sys' is not defined'. So now I can't give the script any cmd arguments :S
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
I was expert with dos 2.1. Now I try to avoid Microsoft.
Code:
>>> a = [1,'two','3rd item at index 2','d','e','f'] # a list
>>> a[0] # the item at first index (index origin 0)
1
>>> a[2]
'3rd item at index 2'
>>> a[:2] # a list of the first 2 items
[1, 'two']
>>> a[1::2] # list of the items at odd indexes
['two', 'd', 'f']
>>>
At startup python guarantees sys.argv is a list of minimum length 1. To display the first two items of sys.argv , I first extended sys.argv by one item to guarantee that it will have two items. Four ways to lengthen a list:
append, extend, +, and insertion.
Code:
>>> a = [1,'two','3rd item at index 2','d','e','f'] # a list
>>> a[0] # the item at first index (index origin 0)
1
>>> a[2]
'3rd item at index 2'
>>> a[:2] # a list of the first 2 items
[1, 'two']
>>> a[1::2] # list of the items at odd indexes
['two', 'd', 'f']
>>> a + ['another list']
[1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'another list']
>>> ['another list'] + a # concatenate (join) two lists
['another list', 1, 'two', '3rd item at index 2', 'd', 'e', 'f']
>>> a[:0]=['at front']
>>> a
['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f']
>>> a[len(a):]=['a','list','at','end']
>>> a
['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end']
>>> a.extend('items')
>>> a
['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 's']
>>> a.append('item')
>>> a
['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 'item']
>>> a[3:3]=['insertion']
>>> a
['at front', 1, 'two', 'insertion', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 'item']
>>>
Anyway, so I made sure there were at least two items in sys.argv and then displayed them, with sys.argv[:2]
Posts: 61
Time spent in forums: 1 Day 1 h 6 m 11 sec
Reputation Power: 1
Thank you immensely b49P23TIvg! Really helped. I couldn't figure out how to fix the 'name 'Skripta' is not defined' error when running the script from cmd...But I'm using Pythonwin now which lets you type the arguments in when you run from the IDE. Is there any drawbacks to entering arguments this way?