ValueError: too many values to unpack,>>>
Steve Holden
steve at holdenweb.com
Thu Sep 27 16:00:33 EDT 2007
Shawn Minisall wrote:
> Fredrik Lundh wrote:
>> Shawn Minisall wrote:
>>
>>
>>> Sorry, it looks like it's on the fourth line with the 3 values on line
>>> 4...its reading line 3 fine
>>>
>>> Traceback (most recent call last):
>>> File "<pyshell#0>", line 1, in <module>
>>> main()
>>> File "I:\COMPUTER PROGRAMMING CLASS\PROJECT #1\project1.py", line 33,
>>> in main
>>> deposit1, deposit2, deposit3 = string.split(line, "\t")
>>> ValueError: too many values to unpack
>>>
>> instead of fumbling around in the dark, try inserting a print statement
>> before the offending line, so you can see what you're trying to unpack:
>>
>> print string.split(line, "\t") # see what it is
>> deposit1, deposit2, deposit3 = string.split(line, "\t")
>>
>> </F>
>>
> I did and it printed everything up until the 3rd line with 3 numbers for
> deposits. I have since figured it out...the teacher put in an extra tab
> after the last value so python thought it was 4 values for three. I
> went back into the file and deleted the extra tab after the 3rd number
> and saved it...now it's working fine.
>
> I'm going to kill her...
>
> ;)
>
Alternatively you could use the additional argument to split() that
tells is the maximum number of splits to [erforms, then strip any
trailing whitespace off before using the values.
That way tou might get an extra mark for not amending the data file.
It's called "defensive programming", and you need t take it seriously
(as does everyone who programs).
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Sorry, the dog ate my .sigline
More information about the Python-list
mailing list