第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將計時器添加到此腳本中的這些文件中的 1 個

將計時器添加到此腳本中的這些文件中的 1 個

溫溫醬 2022-05-19 18:45:35
我有 2 個文件同時打印在同一行。我想將 time.sleep() 添加到 1 個文本文件中。我想延遲此腳本中 1 個文本文件與其他 1 個文本文件打印的時間。我想讓文件創(chuàng)建新行,而不是在它們都打印時打印相同的組合。在哪里添加 time.sleep()?from itertools import izip_longestimport timewith open("file1") as textfile1, open("file2") as textfile2:for x, y in izip_longest(textfile1, textfile2, fillvalue=""):    x = x.strip()    y = y.strip()    print("{0}{1}".format(x, y))
查看完整描述

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))


查看完整回答
反對 回復(fù) 2022-05-19
?
人到中年有點甜

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. 為文件 1 創(chuàng)建 observable

  2. 為文件 2 創(chuàng)建 observable

  3. 創(chuàng)建另一個合并 f1 和 f2 的 observable,它以 1 秒的間隔發(fā)射

  4. 訂閱和打印

  5. 等到它完成


查看完整回答
反對 回復(fù) 2022-05-19
  • 2 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號