Python 输出到同一行覆盖以前的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26584003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:42:32  来源:igfitidea点击:

Output to the same line overwriting previous

python

提问by Prashant sharma

how to get output to the same line by overwriting previous received Timing(countdown) by NTP server. As shown below after each second timing is receiving in next row.

如何通过覆盖 NTP 服务器先前接收到的计时(倒计时)来将输出输出到同一行。如下图所示,每秒钟接收下一行后。

13:35:01

13:35:00

13:34:59

13:34:58

13:34:57

13:34:56

I want timing should be received in same row clearing previous one.

我希望时间应该在清除前一个的同一行中接收。

回答by tobias_k

You can use the "return"-character \rto return to the beginning of the line. In Python 2.x, you'll have to use sys.stdout.writeand sys.stdout.flushinstead of print.

您可以使用“返回”字符\r返回到行的开头。在 Python 2.x 中,您必须使用sys.stdout.writeandsys.stdout.flush而不是print.

import time, sys
while True:
    sys.stdout.write("\r" + time.ctime())
    sys.stdout.flush()
    time.sleep(1)

In Python 3.3, you can use the printfunction, with endand flushparameters:

在 Python 3.3 中,您可以使用print带有endflush参数的函数:

    print(time.ctime(), end="\r", flush=True)

Note, however, that this way you can only replace the last line on the screen. If you want to have a "live" clock in a more complex console-only UI, you should check out curses.

但是请注意,这种方式只能替换屏幕上的最后一行。如果您想在更复杂的仅限控制台的 UI 中使用“实时”时钟,您应该查看curses.

import time, curses
scr = curses.initscr()
scr.addstr(0, 0, "Current Time:")
scr.addstr(2, 0, "Hello World!")
while True:
    scr.addstr(0, 20, time.ctime())
    scr.refresh()
    time.sleep(1)

回答by himnabil

I am using python 2.7

我正在使用 python 2.7

python --version
Python 2.7.12 :: Anaconda 4.1.1 (64-bit)

and I am using this the following function as a hook to display the download progress, by using urllib.urlretrieve

我使用以下函数作为钩子来显示下载进度,通过使用 urllib.urlretrieve

def hook(bcount , bsize, tsize ):
    str = "[ %3d%% of %10d] \r" % (bcount * bsize * 100/tsize , tsize)    
    print str,

urllib.urlretrieve (url, file_name, hook)

Explanation: the \r put the cursor at start of the line , and the comma avoid to print in a new line , if you have the same number of character for each print , this will do the trick

说明: \r 将光标放在行首,逗号避免在新行中打印,如果每次打印的字符数相同,这将起作用

if you are curious about urllib and the hook I am using you'll find the doc https://docs.python.org/2/library/urllib.html

如果您对 urllib 和我使用的钩子感到好奇,您会找到文档https://docs.python.org/2/library/urllib.html

回答by Thirumalai Parthasarathy

This will work like a champ:

这将像冠军一样工作:

print("This text is about to vanish - from first line", end='')
print("\rSame line output by Thirumalai")

output:

输出:

Same line output by Thirumalai from first line

Thirumalai 从第一行输出的同一行

回答by user12875967

Works in python3

在python3中工作

But only for windows

但仅适用于窗户

import os
import time
while True:
    print(time.ctime())
    time.sleep(1)
    os.system('cls')