[Tutor] Open a text file, read and print pattern matching
Steven D'Aprano
steve at pearwood.info
Sun Jan 9 14:48:51 CET 2011
tee chwee liong wrote:
> hi,
>
> there is error when running the code:
>> Traceback (most recent call last):
>> File "C:/Python25/myscript/log/readfile9.py", line 5, in <module>
>> port, channel, lane, eyvt = line.split()
>> ValueError: need more than 2 values to unpack
>
> the error is due to below line code:
>> port, channel, lane, eyvt = line.split()
>
> pls advise.
You have a line with only 2 words, and you are trying to unpack it into
four variables. Four variables on the left, 4 values on the right works:
>>> a, b, c, d = "sentence with four words".split()
>>> print a
sentence
>>> print b
with
>>> print c
four
>>> print d
words
But 4 variables on the left, 3 on the right does not:
>>> a, b, c, d = "sentence with three".split()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack
Likewise:
>>> a, b, c, d = "two words".split()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> a, b, c, d = "word".split()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
--
Steven
More information about the Tutor
mailing list