简体   繁体   English

如何在控制台中的最后一行和倒数第二行之间打印?

[英]How to print between last and second-to-last lines in console?

Using Python 3.4, is there a platform-agnostic method of printing between the last and second-to-last lines? 使用Python 3.4,在最后一行和倒数第二行之间是否存在一种与平台无关的打印方法? What I'm trying to accomplish is basically a progress bar always on the last line. 我想要完成的基本上是最后一行的进度条。 The 2nd line and up are just debug logs. 第二行及以上只是调试日志。 For example: 例如:

[DEBUG] Some info...
[DEBUG] More info...
Download Progress: [####      ] 40% (3 of 20)

I run some downloads in a loop. 我在循环中运行一些下载。 Each iteration of the loop I run this to print the progress bar: 循环的每次迭代我运行它来打印进度条:

def _print_progress_bar(self):
    amt_done = self._count / self._max
    print('\rDownload Progress: [{:20s}] {:.1f}% ({})'.format(
        '#' * int(amt_done * 20),
        amt_done * 100,
        self._padded_counter(' of ')), end='')

Between calls to this method to render the progress bar, I want to insert diagnostic messages above it (I do not want print statements after this to overwrite the progress bar, basically). 在调用此方法来呈现进度条之间,我想在其上面插入诊断消息(我不希望在此之后的print语句覆盖进度条,基本上)。

How can I do this? 我怎样才能做到这一点?

You can get this effect by using the carriage return, '\\r' . 您可以使用回车符'\\r'来获得此效果。 On Windows, this is the character that gets combined with a linefeed ( '\\n' ) to create a new line. 在Windows上,这是与换行符( '\\n' )组合以创建新行的字符。 On Unix systems, a bare linefeed automatically returns the carriage, but '\\r' is still a valid character. 在Unix系统上,裸换行自动返回滑架,但'\\r'仍然是有效字符。 For example, print('hi\\rhello') will print 'hi' , go back to the beginning of the line, and print 'hello' , so only 'hello' ends up actually displayed. 例如, print('hi\\rhello')将打印'hi' ,返回到行的开头,并打印'hello' ,因此实际只显示'hello' You can use end='' to suppress print() 's new line. 你可以使用end=''来压缩print()的新行。 The following function demonstrates what to do - I recommend running it to see what happens for yourself, as the output is, of course, dynamic. 以下函数演示了如何操作 - 我建议运行它来查看自己会发生什么,因为输出当然是动态的。

>>> import time, sys
>>> def doit():
...     print('part 1')
...     print(0, end='')
...     sys.stdout.flush()
...     time.sleep(1)
...     print('\rpart 2')
...     print(1)
...
>>> doit()
part 1
part 2
1

The best way I could find was to use terminal escape codes like this: 我能找到的最好的方法是使用这样的终端转义码:

for i in range(10):
    time.sleep(0.1)
    print('%s\n%s\r\033[F' % (i,i), end="")

This will print a number in one line, then in another line, then move to beginning of the line and one line up to print again. 这将在一行中打印一个数字,然后在另一行中打印,然后移动到行的开头,再一行打印再次打印。 This way you can freely edit lines above you. 这样您就可以自由编辑上方的线条。

This solution however has the potential of not working on Windows. 但是,此解决方案可能无法在Windows上运行。

You need to modify your DEBUG function to erase the line before printing. 您需要修改您的DEBUG功能以在打印前擦除线条。 It is the only platform agnostic way I know. 这是我所知道的唯一与平台无关的方式。

Platform specific way on Windows is to use WinAPI to scroll the upper part of the Window and write exactly to the line above the last. Windows上特定于平台的方法是使用WinAPI滚动窗口的上半部分并精确写入最后一行上方的行。 Maybe a library like https://pypi.python.org/pypi/asciimatics/1.5.0.post1 can help you. 也许像https://pypi.python.org/pypi/asciimatics/1.5.0.post1这样的图书馆可以帮到你。

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

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