清除 Python [Eclipse] 的命令行输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2466866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:31:56  来源:igfitidea点击:

Clear command line output from Python [Eclipse]

pythoneclipseclear

提问by Tomas Aschan

I'm using Eclipse for writing Python, and I want to be able to easily clear the screen. I've seen this question, and tried (among other things suggested there) the following solution

我正在使用 Eclipse 编写 Python,并且希望能够轻松清除屏幕。我已经看到了这个问题,并尝试了(除其他建议外)以下解决方案

import os

def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

but it doesn't entirely solve my problem. Instead of clearing the screen, the routine prints a small square (as if wanting to print an unknown character) to the command output window in Eclipse.

但这并不能完全解决我的问题。该例程不是清除屏幕,而是在 Eclipse 中的命令输出窗口中打印一个小方块(好像想要打印一个未知字符)。

Typing clsin the command line works perfectly fine, as does running a Python script with the above code from command line. But how can I make it look nice in Eclipse as well?

cls在命令行中输入完全正常,就像从命令行使用上述代码运行 Python 脚本一样。但是我怎样才能让它在 Eclipse 中看起来也不错呢?

采纳答案by Joe D

The problem with running it in eclipse is that clsuses ANSI escape sequences to clear the screen. What I mean by this is that to clear the screen, clswrites a string such as "\033[[80;j"to the output buffer. The native console (the one outside of eclipse) interprets this as a command to clear the screen, but the eclipse console doesn't understand it, so just prints a small square as if printing an unknown character.

在 eclipse 中运行它的问题是cls使用 ANSI 转义序列来清除屏幕。我的意思是要清除屏幕,cls将一个字符串写入"\033[[80;j"输出缓冲区。本机控制台(eclipse 之外的那个)将此解释为清除屏幕的命令,但 eclipse 控制台不理解它,因此只打印一个小方块,就像打印未知字符一样。

回答by Ming-Tang

Clearing in outputs is actually not possible. The best hack is to print full of line breaks. IDEs like Eclipse never simulate terminals properly.

清除输出实际上是不可能的。最好的方法是打印完整的换行符。像 Eclipse 这样的 IDE 永远不会正确模拟终端。

回答by Polla A. Fattah

As its just eclipse problem I suppose it will not affect your final product if you want to clean your final users' screen while for eclipse environment there is a little button in top-right of the console screen which enables you from cleaning it.

由于它只是日食问题,我想如果您想清洁最终用户的屏幕,它不会影响您的最终产品,而对于日食环境,控制台屏幕右上角有一个小按钮,可以让您清洁它。