python - 终端 - 如何覆盖多行?

标签 python python-2.7

我想覆盖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/

相关文章:

python - 将 Perl 翻译成 Python

python - 属性错误 : 'MinMaxScaler' object has no attribute 'clip'

python - 从列表中选择相邻的一对(或三合会等)

python - 列表不可散列,但元组可散列?

python - 使用 python Fabric 的 Linux 机器的目录大小

python - Tkinter在带有lambda的for循环中分配按钮命令

python - 从 FTP 检索文件时如何指定本地目标文件夹

java - 可以使用 GCJ 生成可从 Python 调用的库吗?

python-2.7 - 根据不规则的时间间隔合并 Pandas 数据帧

python - 达到 EOF 和使用 Unpack 之间的冲突