简体   繁体   English

如何删除一些输出行?(不使用 os.system('cls'))

[英]How can I delete some output lines?(without using of os.system('cls'))

I want edit some(More than one line) of the last lines in output.我想编辑输出中最后几行的一些(多于一行)。 But I have a lot of lines before them that clearing and reprinting all them slows down my program (I can't use '\\r' Because the lines I want to edit are more than one line).但是我在它们之前有很多行,清除并重新打印所有这些行会减慢我的程序速度(我不能使用'\\r'因为我要编辑的行不止一行)。

print("Lots of lines")
print("Lines to be edited\nLines to be edited")

You can use:您可以使用:

print ("\033[A                             \033[A")

it clears one line from the output.它从输出中清除一行。 Below you can see example of how it works:您可以在下面看到其工作原理的示例:

print("Hello world\n xxx \n xxx")

print ("\033[A                             \033[A")
print ("\033[A                             \033[A")

Output:输出:

Hello world

I wanted to clear two output lines containing xxx so I printed it twice.我想清除包含xxx两条输出行,所以我打印了两次。

You can play with it and make some for loops etc.您可以使用它并制作一些 for 循环等。

Please note that on Windows you may need to enable the ANSI support using the following http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html请注意,在 Windows 上,您可能需要使用以下网址启用 ANSI 支持http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html

"\\033[A" string is interpreted by terminal as move the cursor one line up. "\\033[A"字符串被终端解释为将光标向上移动一行。

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

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