在python3中更新stdout上的打印字符串

2024-09-30 05:28:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python和格式化字符串更新打印到stdout的行。该行正在打印,但在每一个循环中,它都会在同一行上打印,紧挨着上次打印的更新。在

item = ("{}{}{}{}".format(totalAllScraped, 
                          str(" profiles scraped "),
                          (len(sortedurls)), 
                          str(" filtered profiles saved.")))
print(item, sep='', end='', flush=True)

我想把这行打印到stdout,循环的每一次迭代,上一行都会被下一行覆盖。在


Tags: 字符串formatlenstdoutitemprofilessepfiltered
2条回答
import sys

stuff = ("{}{}{}{}".format(totalAllScraped, str(" profiles scraped "), (len(sortedurls)), str(" filtered profiles saved.")))
    sys.stdout.write("\r" + stuff)
    sys.stdout.flush()

这就成功了谢谢

下面是一个简单的例子,Python字符串被打印到stdout被替换

import sys 
import time
for i in range(3):
    message = "testing " + i 
    sys.stdout.write("\r" + stuff)   #The \r is carriage return without a line 
                                     #feed, so it erases the previous line.
    sys.stdout.flush()
    time.sleep(1)

上图:

^{pr2}$

对于Stdout,随后的2次迭代将其删除并替换为“testing 2”,然后“testing 3”。在

相关问题 更多 >

    热门问题