如何在打印下方的文本后覆盖打印语句行?

huangapple go评论68阅读模式
英文:

How to overwrite a print statement line, after printing text below it?

问题

I have translated the provided content into Chinese:

背景

我在寻找适用于此目的的正确的Stack Overflow问题时遇到了困难:
我想要在Python中更新一行打印的内容,该内容是在其他内容打印后计算出来的。"其他内容"应该保留在被更新的行下方。

例如:

first = "hello"
third = ""
print(f'{first} \r{third}', end=" ", flush=True)

second = "somethingelse"
print(second)

if second == "somethingelse":
    third = "world"
else:
    third = "Universe"

应该打印:

hello 
something.

然后更改为:

hello world
something

或者应该打印:

hello
something.

然后更改为:

hello universe
something

重要的是:

  • hello在something之前打印出来。
  • world/universe在hello的同一行上打印出来。
  • something在hello universe/world的下方打印出来。
  • something可以由任意数量的打印行组成。

XY问题

我修改了showme pip包以显示函数的运行时间。我希望打印函数名称,然后是正在运行的函数的输出。并且仍然在与函数名称相同的行上打印运行时间(以减少CLI的混乱)。

问题

如何更新一行内容,该内容是在其他内容打印下方计算出来的?

相关问题:

https://stackoverflow.com/questions/3249524/print-in-one-line-dynamically

https://stackoverflow.com/questions/13094994/how-can-i-print-something-and-then-call-a-function-with-a-print-on-the-same-lin

https://stackoverflow.com/questions/5290994/remove-and-replace-printed-items

https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout

英文:

Context

I have difficulties finding the right stackoverflow question for this purpose:
I would like to update a printed line in python with a value that is computed after something else is printed. the something else should stay below the line that is updated.

For example:

first="hello"
third=""
print(f'{first} \r{third}', end=" ", flush=True)

second="somethingelse"
print(second)

if second == "somethingelse":
    third="world"
else:
    third="Universe"

Should print:

hello 
something.

which is then changed to:

hello world
something

Or it should print:

hello
something.

which is then changed to:

hello universe
something

It is important that:

  • hello is printed before something is printed.
  • world/universe is printed on the same line as hello.
  • something is below the hello universe/world line.
  • something can consist of an arbitrary number of printed lines.

XY-problem

I modified the showme pip package to show the runtimes of functions. And I would like the function name to be printed, followed by the output of the function that is being running. And still print the runtime behind/on the same line as the function name. (To reduce the CLI clutter).

Question

How do I update a line with a value that is computed after other stuff is printed below that line?

https://stackoverflow.com/questions/3249524/print-in-one-line-dynamically

https://stackoverflow.com/questions/13094994/how-can-i-print-something-and-then-call-a-function-with-a-print-on-the-same-lin

https://stackoverflow.com/questions/5290994/remove-and-replace-printed-items

https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout

答案1

得分: 1

以下是已翻译的内容:

不使用curses或类似的东西,只能更新终端输出的最后一行,而不能更新之前的行。

这是一个简单的示例,演示了如何在使用curses打印第二行后更新第一行(sleep函数仅用于使更改更加明显):

import curses
from time import sleep

w = curses.initscr()

w.addstr(0, 0, '第一行')
w.refresh()
w.addstr(1, 0, '第二行')
w.refresh()

sleep(2)

w.addstr(0, 0, '替换第一行')
w.refresh()

sleep(2)

curses.endwin()
英文:

Without using curses or something similar, it is only possible to update the last line of output in a terminal and not the previous ones.

Here is a simple example of how to update the first line after printing the second line with curses (sleep function used only to make the changes more obvious):

import curses
from time import sleep

w = curses.initscr()

w.addstr(0,0, 'first line')
w.refresh()
w.addstr(1, 0, 'second line')
w.refresh()

sleep(2)

w.addstr(0,0, 'replaced first line')
w.refresh()

sleep(2)

curses.endwin()

答案2

得分: 0

I am not quite sure that this answer would satisfy you

first="hello"
third=""
second="sometfirst="hello""
third=""
second="somethingelse"

if second == "somethingelse":
    third="world"
else:
    third="Universe"
    
print(f'{first} {third}', end=" ", flush=True)
print(second)
英文:

i am not quite sure that this answer would satisfy you

first="hello"
third=""
second="sometfirst="hello"
third=""
second="somethingelse"

if second == "somethingelse":
    third="world"
else:
    third="Universe"
    
print(f'{first} {third}', end=" ", flush=True)
print(second)

huangapple
  • 本文由 发表于 2023年5月7日 23:46:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定