def zip_files(*args):
"""Heil iterators!"""
from itertools import ifilter, chain, izip_longest
for line in ifilter(None, chain.from_iterable(izip_longest(*map(open, args)))):
yield line
def sorted_streams(*args):
"""Get sorted stream from a mix of pre-sorted files"""
layer = [(False, open(fn)) for fn in args]
while layer:
layer.sort()
cur, stream = layer[0]
if cur is False:
cur = stream.readline()
if cur == '':
layer = layer[1:]
continue
layer[0] = int(cur), stream
else:
layer[0] = False, stream
yield cur
http://sorhed.livejournal.com/531497.html