Python
21 Feb 2010
 

http://sorhed.livejournal.com/531497.html

 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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