ValueError: need more than 3 values to unpack

Discussion in 'Python' started by Elezar Simeon Papo, Jan 21, 2006.

  1. 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
     
    Elezar Simeon Papo, Jan 21, 2006
    #1
    1. Advertising

  2. Elezar Simeon Papo

    Fuzzyman Guest

    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
     
    Fuzzyman, Jan 21, 2006
    #2
    1. Advertising

  3. Elezar Simeon Papo

    Roy Smith Guest

    "Elezar Simeon Papo" <> 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.
     
    Roy Smith, Jan 21, 2006
    #3
  4. Thanks a lot guys. this solved the problem.
    Best
    Elezar

    ----------------------------------------------------------
    Izraz.com - an open bosnian-croatian-serbian dictionary
    http://izraz.com/Category:Medicinska_stanja
     
    Elezar Simeon Papo, Feb 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    27,408
  2. fscked
    Replies:
    8
    Views:
    370
    Bruno Desthuilliers
    Apr 11, 2007
  3. Replies:
    0
    Views:
    1,479
  4. Shawn Minisall

    ValueError: too many values to unpack,>>>

    Shawn Minisall, Sep 27, 2007, in forum: Python
    Replies:
    9
    Views:
    833
    Bruno Desthuilliers
    Sep 28, 2007
  5. kahmed
    Replies:
    1
    Views:
    494
    John Nagle
    Apr 1, 2011
Loading...

Share This Page