简体   繁体   English

在 Mac OS X 上完全清除终端而不退出 Python 脚本

[英]Fully clear terminal on Mac OS X without exiting Python script

I'm running a simple utility with Python3 that takes text input delimited by comma, tab, or pipe (usually copied from csv) and joins the text around " | " for readability.我正在使用 Python3 运行一个简单的实用程序,它接受由逗号、制表符或管道(通常从 csv 复制)分隔的文本输入,并连接“|”周围的文本以提高可读性。

After an empty input is submitted, the terminal should clear ready for the next input.提交空输入后,终端应清除为下一个输入做好准备。

This works perfectly on Fedora 31, but on OS X the terminal is just moved down to hide the rest of the text, instead of actually clearing it.这在 Fedora 31 上非常有效,但在 OS X 上,终端只是向下移动以隐藏其余文本,而不是实际清除它。 I have tried using "clear", "tput reset" and not sure why this isn't working.我试过使用“clear”、“tput reset”,但不知道为什么这不起作用。 If I use system("tput reset") in Perl instead of Python, it properly clears the terminal on OS X.如果我在 Perl 中使用 system("tput reset") 而不是 Python,它会正确地清除 OS X 上的终端。

from os import system
import re

def clearScreen():
    system('tput reset')


clearScreen()

while True:
    block = []
    line = '_'

    while line:
        line = input('')
        if len(line) > 0:
            block.append(line)
        else:
            clearScreen()

    for row in block:
        print(' | '.join(re.split('[|,\t]', row)) + '\n')

It's a quirk of Mac.这是 Mac 的一个怪癖。

Use system("printf '\\\\33c\\\\e[3J'") instead and it will work.使用system("printf '\\\\33c\\\\e[3J'")代替它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM