ValueError: too many values to unpack,>>>

J. Clifford Dyer jcd at sdf.lonestar.org
Thu Sep 27 16:02:01 EDT 2007


On Thu, Sep 27, 2007 at 09:50:26PM +0200, Bruno Desthuilliers wrote regarding Re: ValueError: too many values to unpack,>>>:
> 
> Shawn Minisall a ?crit :
> > 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...
> 
> You'd better learn how to deal with "this-cant-happen-here" situation, 
> because it's how it is in real-life.
> 

And preferably learn how to deal with it in your code, not in the data that's given to you.  I wouldn't be surprised if your teacher gave you that on purpose.  There's an old maxim which I think applies here: "Be liberal in what you accept and conservative in what you produce."

Note that you have *not* come up with code that handles the dataset given to you by your professor.  Do not expect full marks on this homework assignment, unless you go back and modify your code to handle extraneous tabs at the end of the line.

Cheers,
Cliff




More information about the Python-list mailing list