简体   繁体   English

删除上一行并将其替换为新行?

[英]Delete Previous Line And Replace It With New Line?

My current code is the following: 我当前的代码如下:

for a in xrange (60, 0, -1):  
    b = "Sleeping for " + str(a) + " seconds\r"
    print b,
    sleep(1)

The only issue I have with it is that when it goes from 2 digit numbers to 1 digit numbers it prints secondss instead of seconds. 我唯一遇到的问题是,当它从2位数字变为1位数字时,它将显示秒而不是秒。 How can I modify my code to properly replace the line when a goes from 2 digits to 1. Also, after the script is done printing the "Sleeping for __ seconds" lines am I able to replace that with a line that says "Sleeping for 60 seconds complete" 当a从2位数字变为1时,如何修改代码以正确替换该行。而且,在脚本完成打印“ Sleeping for __ seconds”行之后,我是否可以将其替换为“ Sleeping for 60秒完成”

您需要删除打印行中的逗号才能查看打印输出

How about using string formatting? 如何使用字符串格式?

for a in xrange (60, 0, -1):  
    print "Sleeping for %2d seconds...\r" % a, 
    sleep(1)
print "Sleeping for 60 seconds complete!"

you can print out an equal-length string of spaces to cover it up: 您可以打印出等长的空格字符串来掩盖它:

for a in xrange(60, 0, -1):
    out = "sleeping for {} seconds\r".format(a)
    sys.stdout.write(out)
    sys.stdout.flush()
    time.sleep(1)
    clear = " " * len(out)
    sys.stdout.write(clear + "\r")
print "Done"

you can also add ellipses to out to cover it up: 你也可以椭圆添加到out将其掩盖:

for a in xrange(60, 0, -1):
    out = "sleeping for {} seconds..\r".format(a)
    sys.stdout.write(out)
    sys.stdout.flush()
    time.sleep(1)
print "Done"

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

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