我想覆盖hello
。但是当 \n
被打印时,我无法回到那一行。
那么像程序 htop 这样覆盖许多行的应用程序做了什么。
import sys
print 'hello'
print 'huhu',
print '\r\r\rnooooo\r'
最佳答案
colorama第三方模块支持通过“\x1b[?:?H”命令字符串更改光标的位置。您也可以通过这种方式清屏。
import colorama
colorama.init()
def put_cursor(x,y):
print "\x1b[{};{}H".format(y+1,x+1)
def clear():
print "\x1b[2J"
clear()
put_cursor(0,0)
print "hello"
print "huhu"
#return to first line
put_cursor(0,0)
print "noooooo"
该模块似乎通过导入 ctypes 并调用 windll.kernel32.SetConsoleCursorPosition
来执行此操作。参见 win32.py, line 58 .
关于python - 终端 - 如何覆盖多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864992/