我想做几个语句,给出标准输出,而不看到换行之间的语句。
具体来说,假设我有:
for item in range(1,100):
print item
结果是:
1
2
3
4
.
.
.
如何让它看起来像:
1 2 3 4 5 ...
更好的是,是否可以将单个数字打印在最后一个数字之上,这样一次只有一个数字出现在屏幕上?
我想做几个语句,给出标准输出,而不看到换行之间的语句。
具体来说,假设我有:
for item in range(1,100):
print item
结果是:
1
2
3
4
.
.
.
如何让它看起来像:
1 2 3 4 5 ...
更好的是,是否可以将单个数字打印在最后一个数字之上,这样一次只有一个数字出现在屏幕上?
当前回答
注意:我之所以指出这个解决方案,是因为如果下一次打印的长度小于前一次打印的长度,我所见过的大多数其他解决方案都不起作用。
如果您知道要删除什么,并且可以使用全局变量,那么只需用空格覆盖最后一行。
在打印之前,将字符串的长度存储为' n '。 打印它,但以' \r '结尾(它返回行首)。 下次,在打印信息之前,在该行上打印“n”个空格。
_last_print_len = 0
def reprint(msg, finish=False):
global _last_print_len
# Ovewrites line with spaces.
print(' '*_last_print_len, end='\r')
if finish:
end = '\n'
# If we're finishing the line, we won't need to overwrite it in the next print.
_last_print_len = 0
else:
end = '\r'
# Store len for the next print.
_last_print_len = len(msg)
# Printing message.
print(msg, end=end)
例子:
for i in range(10):
reprint('Loading.')
time.sleep(1)
reprint('Loading..')
time.sleep(1)
reprint('Loading...')
time.sleep(1)
for i in range(10):
reprint('Loading.')
time.sleep(1)
reprint('Loading..')
time.sleep(1)
reprint('Loading...', finish=True)
time.sleep(1)
其他回答
改变
print item
to
print "\033[K", item, "\r",
sys.stdout.flush()
“\033[K”清除到行尾 \r返回行首 flush语句确保它立即显示,以便获得实时输出。
将打印项更改为:
在Python 2.7中,打印项 print(item, end=" "
如果你想动态打印数据,请使用以下语法:
打印(item, sep=' ', end= ", flush=True
“顺便说一下......如何刷新它,所以它打印mi在一个地方,只要改变数字。”
这真的是个棘手的话题。zack所建议的(输出控制台控制代码)是实现这一目标的一种方法。
你可以使用(n)诅咒,但这主要适用于*nix。
在Windows上(这里有有趣的部分),这很少被提及(我不明白为什么),你可以使用Python绑定到WinAPI (http://sourceforge.net/projects/pywin32/默认也带有ActivePython) -这并不难,而且工作得很好。这里有一个小例子:
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
output_handle.WriteConsoleOutputCharacter( i, pos )
time.sleep( 1 )
或者,如果你想使用print(语句或函数,没有区别):
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
print i
output_handle.SetConsoleCursorPosition( pos )
time.sleep( 1 )
Win32console模块可以让你用Windows控制台做更多有趣的事情…我不是WinAPI的忠实粉丝,但最近我意识到我对它的反感至少有一半是由用C语言编写WinAPI代码引起的——python绑定更容易使用。
当然,所有其他的答案都很棒,而且很深奥,但是……如果我想打印上一行呢?或者写多行文本,而不是清除它,再写相同的行?我的解决方案使之成为可能。
打印语句末尾的逗号省略了新行。
for i in xrange(1,100):
print i,
但这不会覆盖。
对于那些像我一样挣扎的人,我提出了以下似乎在python 3.7.4和3.5.2中都可以工作的方法。
I expanded the range from 100 to 1,000,000 because it runs very fast and you may not see the output. This is because one side effect of setting end='\r' is that the final loop iteration clears all of the output. A longer number was needed to demonstrate that it works. This result may not be desirable in all cases, but was fine in mine, and OP didn't specify one way or another. You could potentially circumvent this with an if statement that evaluates the length of the array being iterated over, etc. The key to get it working in my case was to couple the brackets "{}" with .format(). Otherwise, it didn't work.
以下应按原样工作:
#!/usr/bin/env python3
for item in range(1,1000000):
print("{}".format(item), end='\r', flush=True)