python - Merge columns of numbers of different lengths from separate text files into a single csv file -


hi new python programming , cannot seem overcome issue.

i have directory 100 subfolders single text file in each (with no file extension) named same. each file contains single column of numbers of different lengths.

i want merge numbers of each file single csv file numbers each file in separate columns.

so should end matrix of 100 columns of differing lengths of numbers each column corresponds single file.

example of files:

file1

1 15 23 22 10 

file 2

3 55 22 

i have script:

# import modules import glob import csv import sys import itertools  inf = glob.glob("*/*-ambig")  f in inf:     open(f) fin:         open(sys.argv[1], 'w') fout:              writer = csv.writer(fout, delimiter=',',  quotechar='', quoting=csv.quote_none)             headers = ('coverage', )             writer.writerow(headers)              line in fin:                 columns = line.split("\n") # split each column on new line                 writer.writerow(itertools.izip_longest(*columns, fillvalue=[''])) 

however getting error:

traceback (most recent call last):   file "coverage_per_strain.py", line 21, in <module>     writer.writerow(itertools.izip_longest(*columns, fillvalue=[''])) _csv.error: sequence expected 

does have idea wrong code? , can see other errors?

thanks!

csv.writerow expects sequence argument. itertools.izip_longest returning iterator. hence error message.

you should able fix problem with:

writer.writerow(list(itertools.izip_longest(*columns, fillvalue=['']))) 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -