2 回答

TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊
您只能打印 x(不帶換行符),然后休眠并打印 y。
Python 2 解決方案:
import time
from itertools import izip_longest
import sys
with open("file1") as textfile1, open("file2") as textfile2:
for x, y in izip_longest(textfile1, textfile2, fillvalue=""):
x = x.strip()
sys.stdout.write("{0}".format(x))
sys.stdout.flush()
time.sleep(1)
y = y.strip()
sys.stdout.write("{0}".format(y))
sys.stdout.write("\n")
sys.stdout.flush()
Python 3 解決方案
from itertools import zip_longest
import time
with open("file1") as textfile1, open("file2") as textfile2:
for x, y in zip_longest(textfile1, textfile2, fillvalue=""):
x = x.strip()
print("{0}".format(x), end='')
time.sleep(1)
y = y.strip()
print("{0}".format(y))

TA貢獻(xiàn)1895條經(jīng)驗 獲得超7個贊
目前尚不清楚你想要什么,但我想它是這樣的
#! /usr/bin/env python3
from rx import from_, interval, merge, zip
from rx.scheduler import ThreadPoolScheduler
pool = ThreadPoolScheduler(10)
f1 = from_(open('f1.txt'))
f2 = from_(open('f2.txt'))
o1 = merge(
f1,
zip(interval(1.0), f2)
)
o1.subscribe(print, scheduler=pool)
o1.run()
它使用RxPy。并且像這樣工作:
為文件 1 創(chuàng)建 observable
為文件 2 創(chuàng)建 observable
創(chuàng)建另一個合并 f1 和 f2 的 observable,它以 1 秒的間隔發(fā)射
訂閱和打印
等到它完成
添加回答
舉報