473,238 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

ValueError: too many values to unpack,>>>

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

Sep 27 '07 #1
8 7078
On Thu, 27 Sep 2007 12:36:58 -0400, Shawn Minisall wrote:
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
Sep 27 '07 #2
Marc 'BlackJack' Rintsch wrote:
On Thu, 27 Sep 2007 12:36:58 -0400, Shawn Minisall wrote:

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

Sep 27 '07 #3
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>

Sep 27 '07 #4
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...

;)

Sep 27 '07 #5
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.

Sep 27 '07 #6
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

Sep 27 '07 #7
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

Sep 27 '07 #8
J. Clifford Dyer a écrit :
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 :
(snip)
>>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.
Thanks for clarifying this point, which is of course what I meant.

Sep 28 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ahsan Imam | last post by:
Hello All, I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not...
2
by: Mike Spindler | last post by:
I am passing structs via UDP socket to my Python app from an external C program. The structure is made up almost entirely of bit fields. struct example: unsigned int var1 : 3; unsigned int...
0
by: carloschoenberg | last post by:
I have these tables. There are many languages, and many countries. Every country has many (some subset of) languages. create table countries ( country_id serial primary key, country_name...
12
by: yawnmoth | last post by:
Using >> normally shifts bits to the right x number of times, where x is specified by the programmer. As an example, 0x40000000 >> 8 yields 0x00400000. This makes me wonder... why doesn't the...
6
by: jawilson2 | last post by:
Hello, I have some data that stores two 16-bit integers in a 32-bit float, and I would like to extract the data. I want to do something like ...
3
by: Daniel Nogradi | last post by:
Hi list, I have an awk program that parses a text file which I would like to rewrite in python. The text file has multi-line records separated by empty lines and each single-line field has two...
0
by: Syd_Man | last post by:
I have a Gridview that is passing value to a detailsview controls using DefaultValue="EmployeeID = {0}" syntax however if I use the same syntax for the Integer it raises some Conversion error. ...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
3
by: dorandoran | last post by:
function openMyWindow() { var myID = document.getElementById('ctl00_maincontent_txtEmpID').value; //var fname = document.getElementById('<%= lblFullName.ClientID %>').value; //var...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.