覆盖python中以前的打印值?

2024-03-29 13:09:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何覆盖python中以前的“print”值?在

print "hello"
print "dude"
print "bye"

它将输出:

^{pr2}$

但是我想覆盖这个值。在

在这种情况下,输出将是:

bye

Tags: hello情况byeprintpr2dude
3条回答

选中这个curses library,curses库为基于文本的终端提供了一个独立于终端的屏幕绘制和键盘处理工具。例如:

x.py:

from curses import wrapper
def main(stdscr):
    stdscr.addstr(1, 0, 'Program is running..')  
    # Clear screen
    stdscr.clear()  # clear above line. 
    stdscr.addstr(1, 0, 'hello')
    stdscr.addstr(2, 0, 'dude')
    stdscr.addstr(3, 0, 'Press Key to exit: ')
    stdscr.refresh()
    stdscr.getkey()

wrapper(main)
print('bye')

运行它python x.py

import os
print "hello"
print "dude"
if <your condition to bye print >
    os.system('clear')
    print "bye"

您可以使用sys.stdout.write避免在每次调用时print打印的换行符,并使用回车符{}返回行的开头:

import sys
sys.stdout.write("hello")
sys.stdout.write("\rdude")
sys.stdout.write("\rbye")

要覆盖上一句的所有字符,您可能需要添加一些空格。在

在以print为函数的python3或python2上,可以使用end参数:

^{pr2}$

请注意,它在空闲状态下无法工作。在

相关问题 更多 >