用Python在控制台上取消打印一行?

用Python在控制台上取消打印一行?,python,printing,Python,Printing,是否可以操纵已打印到控制台的文本行 比如说, import time for k in range(1,100): print(str(k)+"/"+"100") time.sleep(0.03) #>> Clear the most recent line printed to the console print("ready or not here I come!") 我见过一些在Windows下使用自定义控制台的东西,但我真的很想在命令行上运行,

是否可以操纵已打印到控制台的文本行

比如说,

import time
for k in range(1,100):
     print(str(k)+"/"+"100")
     time.sleep(0.03)
     #>> Clear the most recent line printed to the console
print("ready or not here I come!")
我见过一些在Windows下使用自定义控制台的东西,但我真的很想在命令行上运行,比如does打印,而不需要任何额外的画布

这是否存在?如果没有,为什么不呢


附言:我试图使用诅咒,这导致我在Python之外的命令行行为出现问题。(在从包含诅咒的Python脚本中出错后,我的shell停止打印换行符-不可接受-)。

您要查找的是:

print("{}/100".format(k), "\r", end="")
\r
是回车符,它将光标返回到行首。实际上,打印的内容将覆盖以前打印的文本
end=”“
用于防止打印后
\n
(保持在同一行上)

sonrad10建议的一种更简单的形式:

在这里,我们只是将结束字符替换为
\r
,而不是
\n

在这种情况下,也可以通过以下方式实现:

print "{}/100".format(k), "\r",

您需要的是ANSI命令代码。
您还需要代码来激活ANSI命令代码。我会用Colorama。

使用
诅咒
(Python 3.4+)模块。

最简单的方法(至少对于Python 2.7)是使用以下语法:

print 'message', '\r',
print 'this new message now covers the previous'

请注意第一次打印结束时的额外“,”。这使打印保持在同一行上。同时,'\r'将打印内容放在该行的开头。所以第二个print语句覆盖了第一个。

@kpie您使用的是python2,在这种情况下,语法是
print“{}/100.format(k),“\r”,
我相信您可以使用
print({}/100.format(k),end=“\r”)
作为此代码的更简单版本
print 'message', '\r',
print 'this new message now covers the previous'