Q:

ValueError: too many values to unpack (while using a dict with tuple keys)

I want to compare two .txt files. The first file is a "key" with three values separated by tabs (i.e. "Item Number" "Response" and "Code"). The second file contains two values separated by tabs ("Item Number" and "Response"). I need my program to search the first file, find any matching "Item Number/Response" pairs with the second file, then output the correct "Code." If there is not a match, then I would like the output to just be a space (" "). I am not a programmer, but figuring this out would greatly decrease time I spend on certain tasks at work. 
I found this thread to be helpful in setting up my code. I want to accomplish the same thing.
file 1, "Key.txt":  
1   dog C  
2   cat C  
3   bird    C  
4   pig C  
5   horse   C  
1   cat Sem  
2   bat TA  
3   animal  Super  
4   panda   M  
5   pencil  U  

file2, "Uncoded.txt":  
4   pig  
3   animal  
5   bird  
2   bat  
2   cat  
0   
1   fluffy  
0   dog  
1   

desired output:  
4   pig  C  
3   animal  Super  
5   bird    
2   bat  TA  
2   cat  C  
0     
1   fluffy    
0   dog    
1     

Below is the code I have:
f1 = open("Key.txt")  
f2 = open("Uncoded.txt")    
d = {}  

while True:  
    line = f1.readline()  
    if not line:  
        break  
    c0,c1,c2 = line.split('\t')  
    d[(c0,c1)] = (c0,c1,c2)  
while True:  
    line = f2.readline()  
    if not line:  
        break  
    c0,c1 = line.split('\t')  
    if (c0,c1) in d:  
        vals = d[(c0,c1)]  
        print (c0, c1, vals[1])  

f1.close()  
f2.close()

If I try to separate the lines with tabs ('\t') then I get a ValueError: too many values to unpack at the line "c0,c1,c2 = line.split('\t')"
Thanks so much for any insight or help!

A:

The issue you're having is that one of the lines in one of your files doesn't have the number of items on it that you expect. A possible cause of that is an extra newline (probably at the end of the file). Python will see that as a line with only a newline character in it after the last real line. Your logic will fail when it can't split the empty line into three parts.
One way to work around the issue is to split into a single variable, without unpacking the values. Then you can check how many items were split, and only go on with the unpacking if it is the expected amount:
while True:  
    line = f1.readline()  
    if not line:  
        break  
    vals = line.split('\t')  # don't unpack immediately
    if len(val) == 3:        # check you got the expected number of items
        c0, c1, c2 = vals    # unpack only if it will work
        d[(c0,c1)] = (c0,c1,c2)
    else:
        print("got unexpected number of values: {}".format(vals) # if not, report the error

It's unrelated to your error, but you can simplify your loop quite a bit if you want to, by using a for loop instead of a while loop. File objects are iterable, yielding the lines of the file (just like you get from readline(). The nicest thing about it is that you don't need to look for the end of the file yourself, the iteration just ends when the file is exhausted:
for line in f1:    # this does the same thing as the first four lines in the code above
    ...

