python - Using generators to read two files in time priority -
i read on generators , wondering how use generators in this:
there 2 files , each file has time column. each file sorted ascending time, , i'm looking grab lines in these files using time priority. instead of writing unsophisticated expression(see below) wondering if creating generator next() better/appropriate way read these 2 files in time priority.
for line1 in file1: do_something try: if time1<time2: do_something continue else: do_something except: pass line2 in file2: do_something if time2>time1: break
use heapq.merge
def generate_timeline(file): line in file: time1 = extract_time_from_line(line) yield time1, line (time1, line) in heapq.merge(generate_timeline(file1), generate_timeline(file2)): process(line)
Comments
Post a Comment