2 回答

TA贡献1799条经验 获得超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贡献1895条经验 获得超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 创建 observable
为文件 2 创建 observable
创建另一个合并 f1 和 f2 的 observable,它以 1 秒的间隔发射
订阅和打印
等到它完成
添加回答
举报