简体   繁体   中英

Python 2.7.1 ValueError: too many values to unpack

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] 

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):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM