我想做几个语句,给出标准输出,而不看到换行之间的语句。

具体来说,假设我有:

for item in range(1,100):
    print item

结果是:

1
2
3
4
.
.
.

如何让它看起来像:

1 2 3 4 5 ...

更好的是,是否可以将单个数字打印在最后一个数字之上,这样一次只有一个数字出现在屏幕上?


当前回答

为了让数字相互覆盖,你可以这样做:

for i in range(1,100):
    print "\r",i,

只要把数字打印在第一列,这就可以工作。

编辑: 这里有一个版本,即使没有打印在第一列,也可以工作。

prev_digits = -1
for i in range(0,1000):
    print("%s%d" % ("\b"*(prev_digits + 1), i)),
    prev_digits = len(str(i))

我应该指出,这段代码经过测试,在Windows上的Python 2.5中,在Windows控制台中工作得很好。根据其他一些方法,可能需要刷新stdout才能查看结果。YMMV。

其他回答

“顺便说一下......如何刷新它,所以它打印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 range(1,100):
    print "\r",i,

只要把数字打印在第一列,这就可以工作。

编辑: 这里有一个版本,即使没有打印在第一列,也可以工作。

prev_digits = -1
for i in range(0,1000):
    print("%s%d" % ("\b"*(prev_digits + 1), i)),
    prev_digits = len(str(i))

我应该指出,这段代码经过测试,在Windows上的Python 2.5中,在Windows控制台中工作得很好。根据其他一些方法,可能需要刷新stdout才能查看结果。YMMV。

注意:我之所以指出这个解决方案,是因为如果下一次打印的长度小于前一次打印的长度,我所见过的大多数其他解决方案都不起作用。

如果您知道要删除什么,并且可以使用全局变量,那么只需用空格覆盖最后一行。

在打印之前,将字符串的长度存储为' 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)

对于那些像我一样挣扎的人,我提出了以下似乎在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)

使用print item,使print语句省略换行符。

在python3中,它是print(item, end=" ")。

如果你想让每个数字都显示在相同的位置,使用例如(Python 2.7):

to = 20
digits = len(str(to - 1))
delete = "\b" * (digits + 1)
for i in range(to):
    print "{0}{1:{2}}".format(delete, i, digits),

在Python 3中,这有点复杂;这里你需要刷新系统。Stdout或者在循环结束之前它不会打印任何东西:

import sys
to = 20
digits = len(str(to - 1))
delete = "\b" * (digits)
for i in range(to):
   print("{0}{1:{2}}".format(delete, i, digits), end="")
   sys.stdout.flush()