0
votes

I'm trying to resolve this python syntax in version 2.7.1, but I keep receiving the ValueError: too many values to unpack. Does anyone know what I may be doing wrong?

import sys  

number_of_outfiles = 100 

if __name__ == "__main__":  
    k = []  
    for i in range(number_of_outfiles):  
        k.append(open('/Users/rootrune/Documents/grand' + str(i) + '.csv','w'))  
    with open(sys.argv[1]) as inf:  
        for i, line in inf:  
            if line[-1] == '\n': line = line[:-1]  
            if i == 0:  
                headers = line  
                [x.write(headers + '\n') for x in k]  
            else:  
                k[i % number_of_outfiles].write(line + '\n')  
    [x.close() for x in k] 
1

1 Answers

2
votes

File handles are iterators over strings (lines). Your code indicates that you expect (index, line) pairs as they would be produced by enumerate. So

for i, line in inf:

should probably be:

for i, line in enumerate(inf):