473,396 Members | 2,011 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,396 software developers and data experts.

ValueError: need more than 3 values to unpack

Hello All,

I have a tab separated input file (data.txt) in text format - the file
looks like this

SCHOOL DEPART1 DEPART2 DEPART3
Harvard Economics Mathematics Physics
Stanford Mathematics Physics
Berkeley Physics
U.C.L.A Biology Genetics

I have to utilize Python and to generate four files based on my input
file. The files would have this structure:

First File
===================================
Filename: Harvard.txt
Harvard
Economics
Mathematics
Physics ===================================

Second File
===================================
Filename: Stanford.txt
Stanford
Mathematics
Physics

===================================

The same pattern would follow for files 3 and 4.
I came up with this code,

==========================================
#! python # -*- coding: cp1252 -*-

InputFILENAME = "data.txt" #Name of the data fule containg all records
OutputFileExtension = ".txt" #File extension of output files

for line in open(InputFILENAME):

SCHOOL, DEPART1, DEPART2, DEPART3 = line.split('\t')
f = open(SCHOOL.strip() + OutputFileExtension, "w+")

f.write("" +SCHOOL.strip() +"\n")
f.write(">> " +DEPART1.strip() +"\n")
f.write(">> " +DEPART2.strip() +"\n")
f.write(">> " +DEPART3.strip() +"\n")
f.close() #InputFILENAME

# end
==========================================
I am having problems as the program will not work if all three DEPART
values are not present. If I populate all DEPART values for all records
program functions without issues.

When I run the check module, I get the following:
Traceback (most recent call last):
File "D:\Documents and
Settings\administrator\Desktop\code\test\testcode. py", line 8, in ?

SCHOOL, DEPART1, DEPART2, DEPART3 = line.split('\t')
ValueError: need more than 3 values to unpack


How do I solve this problem and change the program so it does not stop
if a data record has less than three DEPART values.

Thanks for your help!

Elezar

Jan 21 '06 #1
3 24785

Elezar Simeon Papo wrote:
Hello All,

I have a tab separated input file (data.txt) in text format - the file
looks like this

SCHOOL DEPART1 DEPART2 DEPART3
Harvard Economics Mathematics Physics
Stanford Mathematics Physics
Berkeley Physics
U.C.L.A Biology Genetics

I have to utilize Python and to generate four files based on my input
file. The files would have this structure:

First File
===================================
Filename: Harvard.txt
Harvard
Economics
Mathematics
Physics ===================================

Second File
===================================
Filename: Stanford.txt
Stanford Mathematics
Physics ===================================

The same pattern would follow for files 3 and 4.
I came up with this code,

==========================================
#! python # -*- coding: cp1252 -*-

InputFILENAME = "data.txt" #Name of the data fule containg all records
OutputFileExtension = ".txt" #File extension of output files

for line in open(InputFILENAME):

SCHOOL, DEPART1, DEPART2, DEPART3 = line.split('\t')
f = open(SCHOOL.strip() + OutputFileExtension, "w+")

f.write("" +SCHOOL.strip() +"\n")
f.write(">> " +DEPART1.strip() +"\n")
f.write(">> " +DEPART2.strip() +"\n")
f.write(">> " +DEPART3.strip() +"\n")
f.close() #InputFILENAME

# end
==========================================
I am having problems as the program will not work if all three DEPART
values are not present. If I populate all DEPART values for all records
program functions without issues.

When I run the check module, I get the following: Traceback (most recent call last):
File "D:\Documents and
Settings\administrator\Desktop\code\test\testcode. py", line 8, in ?

SCHOOL, DEPART1, DEPART2, DEPART3 = line.split('\t')
ValueError: need more than 3 values to unpack

How do I solve this problem and change the program so it does not stop
if a data record has less than three DEPART values.

Thanks for your help!


Sounds like homework ;-)

if you do :

values = line.split('\t')

Then you have a list with all the separate values in it.
You can then do the first line :

f.write("" +values[0].strip() +"\n")

Then the rest...

for entry in values[1:]:
f.write(">> " +entry.strip() +"\n")

This will work whatever the number of values. It won't report any badly
formed lines to you though.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml Elezar


Jan 21 '06 #2
"Elezar Simeon Papo" <el**************@yahoo.com> wrote:
SCHOOL, DEPART1, DEPART2, DEPART3 = line.split('\t')
ValueError: need more than 3 values to unpack

How do I solve this problem and change the program so it does not stop
if a data record has less than three DEPART values.


The problem is that when you unpack a sequence, you must have exactly the
same number of variables as you have values. The idiom I use in a
situation where I'm not sure how many values I've got in the sequence is:

words = line.split('\t')+ 4 * [None]
firstFourWords = words[0:3]
SCHOOL, DEPART1, DEPART2, DEPART3 = firstFourWords

The first line generates a list of words which is guaranteed to have at
least 4 elements in it. The second line gets the first four of those
words, and the last line unpacks them (in real life, I'd probably have
combined the second and third lines, but I spread it out here for
educational purposes).

If line is "HardKnocks\tFoo", you'll end up unpacking ["HardKnocks", "Foo",
None, None]. Depending on your application, you may want to pad with empty
strings instead of None.

It would be convenient if string.split() took an optional 'minsplit'
argument as well as a 'maxsplit'. Then you could have just done:

school, dept1, dept2, dept3 = line.split ('\t', 4, 4)

but, alas, it doesn't work that way.
Jan 21 '06 #3
Thanks a lot guys. this solved the problem.
Best
Elezar

----------------------------------------------------------
Izraz.com - an open bosnian-croatian-serbian dictionary
http://izraz.com/Category:Medicinska_stanja

Feb 7 '06 #4

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

Similar topics

5
by: hikums | last post by:
How to get the header values in the sql with union. For example, select deptno as dep,deptname as dname,location as loc,mgrno as mgr from dept union select empno,empname,'Employee','' from emp...
3
by: Ron | last post by:
I need a way to read the hex values of an old DBase3 data file. Can anyone give me some info regarding how to grab hex values out of a file? Not sure exactly how to do this. Thanks for your...
9
by: JD | last post by:
Hi guys What if I need to return two values from a function. What's the best way to do it? Function is to load a text file into memory like so: -----8<----- char *LoadFile(char *infile)
2
by: k.retheesh | last post by:
Hi, I am very new to pyton, during my adventures journey I got the following error message which am not able to solve, can somebody help me. I was trying to format my output in a readable way, ...
2
by: mumbaimacro | last post by:
hi i have two combo boxes in a form with values from a table. i need a report to be opened by a button from the form ,The Report should show the values selected in the combo boxes in the form...
0
by: laxmikiran.bachu | last post by:
ValueError: need more than 1 value to unpack . This is the error I get when I am using pyRXPU in the one of the scripts to get the strings from an application. Can anybody throw some light on...
8
by: Shawn Minisall | last post by:
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...
4
by: f0rd | last post by:
Hi everybody, I have a python script I developed for work, that organises sound samples and out puts them to a text file. Here is an example input file:Sound001,15,6 Sound002,15,6 Sound003,30,3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.