ValueError: too many values to unpack,>>>

S

Shawn Minisall

I am trying to read a few lines of a file with multiple values, the rest
are single and are reading in fine.

With the multiple value lines, python says this "ValueError: too many
values to unpack"

I've googled it and it says that happens when you have too few or too
many strings that don't match with the variables in number your trying
to assign them too. Below are the lines in reading in:

line 3 - 19.18 29.15 78.75 212.10
line 4 - 100 20 410.29

And this is the code I'm using:

#read withdrawls from file on line3
line = infile.readline()

#split withdrawls up
withdraw1, withdraw2, withdraw3, withdraw4 = string.split(line, "\t")

#read deposits from file on line4
line = infile.readline()
#split deposits up
deposit1, deposit2, deposit3 = string.split(line, "\t")

I have 4 strings to match line 3 and 3 to match the 3 on line 4...any
thoughts?

thx
 
M

Marc 'BlackJack' Rintsch

With the multiple value lines, python says this "ValueError: too many
values to unpack"

I've googled it and it says that happens when you have too few or too
many strings that don't match with the variables in number your trying
to assign them too. Below are the lines in reading in:

line 3 - 19.18 29.15 78.75 212.10
line 4 - 100 20 410.29

And this is the code I'm using:

#read withdrawls from file on line3
line = infile.readline()

#split withdrawls up
withdraw1, withdraw2, withdraw3, withdraw4 = string.split(line, "\t")

#read deposits from file on line4
line = infile.readline()
#split deposits up
deposit1, deposit2, deposit3 = string.split(line, "\t")

I have 4 strings to match line 3 and 3 to match the 3 on line 4...any
thoughts?

First thought is to find out which of the two lines triggers the
exception. This information is part of the full traceback.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Shawn Minisall

Marc said:
First thought is to find out which of the two lines triggers the
exception. This information is part of the full traceback.

Ciao,
Marc 'BlackJack' Rintsch

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
 
F

Fredrik Lundh

Shawn said:
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>
 
S

Shawn Minisall

Fredrik said:
Shawn Minisall wrote:



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...

;)
 
B

Bruno Desthuilliers

Shawn Minisall a écrit :
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.
 
S

Steve Holden

Shawn said:
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
 
J

J. Clifford Dyer

Shawn Minisall a ?crit :

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
 
D

Dennis Lee Bieber

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.
Which is what I'd expect for, say, a tab-separated file created by a
block of entries in an Excel table.

IOWs, both lines have four fields, but the second just happens to
have a null entry for the fourth field.

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bruno Desthuilliers

J. Clifford Dyer a écrit :
And preferably learn how to deal with it in your code, not in the
data that's given to you.

Thanks for clarifying this point, which is of course what I meant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top