Skip to main content
 首页 » 编程设计

python之在 python3 中更新标准输出上的打印字符串

2025年01月19日30dflying

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

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

我想将该行打印到标准输出,循环的每次迭代,前一行被下一行覆盖。

请您参考如下方法:

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

无论如何,这还是成功了