I've searched and found code below that can interleave 2 files line by line into new one.
from itertools import izip_longest
from contextlib import nested
with nested(open('foo'), open('bar')) as (foo, bar):
for line in (line for pair in izip_longest(foo, bar)
for line in pair if line):
print line.strip()
I have multiple files and like to have 2 or more consecutive lines to interleave one after another. I like to be able to choose number of lines depend on the job. Total number of lines in each file may not be the same, but the pattern for number of lines for each element on all files are always the same. How can I achieve my goal?
input:
fileA
lineA1
lineA2
lineA3
......
fileB
lineB1
lineB2
lineB3
......
For 2 lines output:
lineA1
lineA2
lineB1
lineB2
.....
For 3 lines output:
lineA1
lineA2
lineA3
lineB1
lineB2
lineB3
....
Thank you.
@xealits Thanks a million. Your codes work like a champ. Have a nice day!