我想运行一个脚本,它基本上显示如下输出:
Installing XXX... [DONE]
目前,我打印安装XXX…首先,然后打印[完成]。
我怎么能打印安装xxx…和[DONE]在同一行?
关于在同一行上写入新消息,替换之前的内容的具体问题,请参见如何将先前的打印内容覆盖到标准输出?。这里的大多数答案都把这个问题理解为在当前行的末尾写新的文本。
有关使用单个打印一次输出多个内容的问题,请参见如何在同一行上一次性打印多个内容(固定文本和/或变量值)。
我想运行一个脚本,它基本上显示如下输出:
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()有一个内置参数“end”,默认设置为“\n” 调用print("This is America")实际上是在调用print("This is America", end = "\n")。 一个简单的方法是调用print("This is America", end ="")
这是一个非常古老的线程,但这里有一个非常彻底的答案和示例代码。
\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()
使用sys.stdout。写(“安装XXX……')和sys.stdout.write('Done')。通过这种方式,如果您想重新创建打印功能,就必须手动添加“\n”新行。我认为没有必要为此使用诅咒。
Python 3解决方案
print()函数接受一个结束参数,默认为\n(新行)。将其设置为空字符串可防止它在行尾发出新行。
def install_xxx():
print("Installing XXX... ", end="", flush=True)
install_xxx()
print("[DONE]")
Python 2解决方案
在print()行末尾使用逗号可以防止print()输出新的行(您应该注意,在输出的末尾会有一个额外的空格)。
def install_xxx():
print "Installing XXX... ",
install_xxx()
print "[DONE]"
在Quora上找到了这个帖子,上面的例子对我很有用(python 3), 这更接近于我需要它(即擦除整个前一行)。
他们提供的例子是:
def clock():
while True:
print(datetime.now().strftime("%H:%M:%S"), end="\r")
如果要在同一行打印,就像其他人建议的那样,只需使用end=""