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

Installing XXX...               [DONE]

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

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


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

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


当前回答

您应该使用退格'\r'或('\x08')字符返回控制台输出中的先前位置

Python 2 +:

import time
import sys

def backspace(n):
    sys.stdout.write((b'\x08' * n).decode()) # use \x08 char to go back   

for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    sys.stdout.write(s)                     # just print
    sys.stdout.flush()                      # needed for flush when using \x08
    backspace(len(s))                       # back n chars    
    time.sleep(0.2)                         # sleep for 200ms

Python 3:

import time   

def backline():        
    print('\r', end='')                     # use '\r' to go back


for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    print(s, end='')                        # just print and flush
    backline()                              # back to the beginning of line    
    time.sleep(0.2)                         # sleep for 200ms

这段代码将在一行中从0%计算到100%。最终值为:

> python test.py
100%

关于这种情况下flush的其他信息:为什么python打印包含'end='参数的语句在while循环中表现不同?

其他回答

您应该使用退格'\r'或('\x08')字符返回控制台输出中的先前位置

Python 2 +:

import time
import sys

def backspace(n):
    sys.stdout.write((b'\x08' * n).decode()) # use \x08 char to go back   

for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    sys.stdout.write(s)                     # just print
    sys.stdout.flush()                      # needed for flush when using \x08
    backspace(len(s))                       # back n chars    
    time.sleep(0.2)                         # sleep for 200ms

Python 3:

import time   

def backline():        
    print('\r', end='')                     # use '\r' to go back


for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    print(s, end='')                        # just print and flush
    backline()                              # back to the beginning of line    
    time.sleep(0.2)                         # sleep for 200ms

这段代码将在一行中从0%计算到100%。最终值为:

> python test.py
100%

关于这种情况下flush的其他信息:为什么python打印包含'end='参数的语句在while循环中表现不同?

下面是@Vadim-Zin4uk从3.0版本衍生出来的2.7兼容版本:

Python 2

import time

for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    print '{0}\r'.format(s),                # just print and flush

    time.sleep(0.2)

因此,3.0提供的解决方案看起来有点臃肿。例如,backspace方法不使用整型参数,可以完全取消。

Python 3

import time

for i in range(101):                        # for 0 to 100
    s = str(i) + '%'                        # string for output
    print('{0}\r'.format(s), end='')        # just print and flush

    time.sleep(0.2)                         # sleep for 200ms

这两种方法都经过了测试,并且有效。

Print()有一个内置参数“end”,默认设置为“\n” 调用print("This is America")实际上是在调用print("This is America", end = "\n")。 一个简单的方法是调用print("This is America", end ="")

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

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

使用sys.stdout。写(“安装XXX……')和sys.stdout.write('Done')。通过这种方式,如果您想重新创建打印功能,就必须手动添加“\n”新行。我认为没有必要为此使用诅咒。