Reputation: 3520
I have a list of files ['file_a.txt', 'file_b.txt', 'file_c.txt' ....]
I want to read one line from each file and generate a new line by adding them together.
The output should look like:
file_a_line_1 file_b_line_1 file_c_line_1....
file_a_line_2 file_b_line_2 file_c_line_2....
Can zip
be used to do this?
Upvotes: 2
Views: 4963
Reputation: 2662
Something like this might work. You'd open all the files, then read one line at a time from each of them. It's unclear what you want to do when one file has no lines left (stop entirely), keep going until all lines have no lines left?
file_list = ['file1', 'file2', 'file3']
fps = []
for fn in file_list:
fps.append(open(fn, 'w'))
curLine = 'start'
while curLine:
curLine = ''
for fp in fps:
myLine = fp.readline()
if myLine:
curLine += myLine + ' '
else:
break #can do what you want when you run out
for fp in fps:
fp.close()
Remember to close your file handlers.
Upvotes: 1
Reputation: 27331
from itertools import zip_longest
files = [open(filename) for filename in file_list]
for lines in zip_longest(*files, fillvalue=''):
print(" ".join(lines))
This should also work when the files don't have the same length. I would use izip_longest, if you're on Python 2, instead.
This will leave several spaces between values if some files are exhausted, so you might want to do more complicated stuff than the join, but that's the easier part.
Upvotes: 3