我想运行一个脚本,它基本上显示如下输出:

Installing XXX...               [DONE]

目前,我打印安装XXX…首先,然后打印[完成]。

我怎么能打印安装xxx…和[DONE]在同一行?


关于在同一行上写入新消息,替换之前的内容的具体问题,请参见如何将先前的打印内容覆盖到标准输出?。这里的大多数答案都把这个问题理解为在当前行的末尾写新的文本。

有关使用单个打印一次输出多个内容的问题,请参见如何在同一行上一次性打印多个内容(固定文本和/或变量值)。


当前回答

这是一个非常古老的线程,但这里有一个非常彻底的答案和示例代码。

\r是ASCII字符集回车的字符串表示形式。它与八进制015 [chr(0o15)]或十六进制0d [chr(0x0d)]或十进制13 [chr(13)]相同。看man ascii,无聊的阅读。它(\r)是一种相当可移植的表示法,并且易于人们阅读。它非常简单地意味着将打字机上的架子一直移动到起点,而不前进纸张。这是CRLF的CR部分,意思是回车和换行。

print()是Python 3中的一个函数。在Python 2(任何你感兴趣的版本)中,可以通过从__future__模块导入其定义来将print强制导入函数。print函数的好处是,您可以指定在末尾打印什么,覆盖\n的默认行为,在每次print()调用的末尾打印换行符。

sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.

还有一个样本。请注意,这在Python 2或3中运行得很好。

from __future__ import print_function

import sys
import time

ANS = 42
FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

for i in range(1, ANS + 1):
    if i in FACTORS:
        print('\r{0:d}'.format(i), end='')
        sys.stdout.flush()
        time.sleep(ANS / 100.0)
else:
    print()

其他回答

如果你想覆盖前一行(而不是不断地添加),你可以在print语句的末尾将\r与print()结合使用。例如,

from time import sleep

for i in xrange(0, 10):
    print("\r{0}".format(i)),
    sleep(.5)

print("...DONE!")

将计数0到9,取代控制台中旧的数字。“…DONE!”将与最后一个计数器9打印在同一行。

在OP的情况下,这将允许控制台以“进度条”的形式显示安装完成百分比,您可以在其中定义开始和结束字符的位置,并在两者之间更新标记。

print("Installing |XXXXXX              | 30%"),

在Quora上找到了这个帖子,上面的例子对我很有用(python 3), 这更接近于我需要它(即擦除整个前一行)。

他们提供的例子是:

def clock():
   while True:
       print(datetime.now().strftime("%H:%M:%S"), end="\r")

如果要在同一行打印,就像其他人建议的那样,只需使用end=""

最简单的:

Python 3

    print('\r' + 'something to be override', end='')

这意味着它将把光标返回到开头,然后打印内容并在同一行中结束。如果在循环中,它将在开始时的相同位置开始打印。

Python将换行符作为输出的结束符。使用end=' ' for python3 for print方法添加一个空格而不是换行符。对于python2,在print语句的末尾使用逗号。

print('Foo', end=' ')
print('Bar')

Python 3中的解决方案。X具体:

当我需要这样做的时候,我通常只用

end=' '

例如:

# end='' ends the output with a <space> 
print("Welcome to" , end = ' ') 
print("stackoverflow", end = ' ')

输出如下:

Welcome to stackoverflow

end=中的空格可以替换为任何字符。例如,

print("Welcome to" , end = '...') 
print("stackoverflow", end = '!')

输出如下:

Welcome to...stackoverflow!