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
 
G

George Sakkis

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

Just print out the split without unpacking to see how many elements
there are actually present:

print string.split(line, "\t")

By the way, most functions of the string module are deprecated in
favor of string methods; the above is better written as

print line.split("\t")


HTH,
George
 
Z

Zentrader

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

A string.split returns a list so you don't have to know how many
elements there are beforehand. A simple example
withdraw = line.split("\t")
if len(withdraw) == 3:
match_3(withdraw)
elif len(withdraw) == 4::
match_4(withdraw)
else:
print "line.split() is not 3 or 4", withdraw
 
P

Pablo Ziliani

Zentrader said:
A string.split returns a list so you don't have to know how many
elements there are beforehand. A simple example
withdraw = line.split("\t")
if len(withdraw) == 3:
match_3(withdraw)
elif len(withdraw) == 4::
match_4(withdraw)
else:
print "line.split() is not 3 or 4", withdraw

Right, and then because error was caused for an unexpected extra tab you
made the deposit pass as a withdrawal...
Not everybody trades just Zen :)

(tip: the unpacking mystery has been solved earlier this day)

Regards,
Pablo
 

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