简体   繁体   English

如何动态刷新多行output

[英]How to refresh the multi-line output dynamically

I want to refresh some info dynamically(just like progress bar), I can do it with following code我想动态刷新一些信息(就像进度条一样),我可以用下面的代码来完成

#! /usr/bin/env python
import sys
import time

print "start the output"
def loop():
    i = 0

    while 1:
        i += 1
        output = "\rFirst_line%s..." % str(i) 
        sys.stdout.write(output)        
        sys.stdout.flush()
        time.sleep(1)
loop()

It could only output single_line info dynamically, When add '\n' into output, It couldn't work as expect.动态只能显示output单行信息,在output中添加'\n'时,无法正常工作。

output = "\rFirst_line%s...\n" % str(i)

Any way could help it to refresh multi_line content?有什么办法可以帮助它刷新 multi_line 内容吗?

I also had that situation, and I finally got a idea to solve this ;D 我也有这种情况,我终于有了解决这个问题的想法; D.

reprint- A simple module for Python 2/3 to print and refresh multi line output contents in terminal reprint-一个简单的模块,用于Python 2/3打印和刷新终端中的多行输出内容

You can simply treat that output instance as a normal dict or list (depend on which mode you use). 您可以简单地将该output实例视为普通dictlist (取决于您使用的模式)。 When you modify that content in the output instance, the output in terminal will automatically refresh :D output实例中修改该内容时,终端中的输出将自动刷新:D

Here is a example: 这是一个例子:

from reprint import output
import time
import random

print "start the output"

with output(initial_len=3, interval=0) as output_lines:
    while True:
        output_lines[0] = "First_line  {}...".format(random.randint(1,10))
        output_lines[1] = "Second_line {}...".format(random.randint(1,10))
        output_lines[2] = "Third_line  {}...".format(random.randint(1,10))
        time.sleep(0.5)

你可以用诅咒来做,但这是非常重要的。

There is no way to do that system-independently (when saying "system" I mean not just OS, but also terminal app and its setup as well) since there is no standard escape sequence that would move cursor up. 没有办法独立地执行该系统(当说“系统”我不仅指操作系统,而且还指终端应用程序及其设置),因为没有标准的转义序列会将光标向上移动。 As for system-dependent ways, they may exist for your configuration, but more information about your system is needed. 至于与系统相关的方式,它们可能适用于您的配置,但需要有关系统的更多信息。 And anyway, usually it is not a good idea to use such features. 无论如何,通常使用这些功能并不是一个好主意。

PS Hope you are not going to redirect your program's output to a file. PS希望您不会将程序的输出重定向到文件。 If redirected to a file, such progress indicators produce terrible output. 如果重定向到文件,此类进度指示器会产生可怕的输出。

Simply do:简单地做:

import time
import random

while True:
    print(f'''First Line {random.randint(1,10)}
Second Line {random.randint(1,10)}        
Third Line  {random.randint(1,10)}''')
    sys.stdout.write("\x1b[1A"*3) # Cursor up 3 lines
    time.sleep(0.5)

write '\\b' to the console: 将'\\ b'写入控制台:

import sys
import time

print "start the output"
def loop():
    i = 0
    output = "\rFirst_line%s..." % str(0) 
    sys.stdout.write(output)
    while 1:
        sys.stdout.write('\b'*len(output))
        i += 1
        output = "\rFirst_line%s..." % str(i) 
        sys.stdout.write(output)        
        sys.stdout.flush()
        time.sleep(1)
loop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM