ValueError: need more than 3 values to unpack

E

Elezar Simeon Papo

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

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

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
 
F

Fuzzyman

Elezar said:
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
===================================

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

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
 
R

Roy Smith

Elezar Simeon Papo said:
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.
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top