如何覆盖上一次的打印结果

2015-05-29 17:17:45 +08:00
 cc7756789
就比如像下面这种循环结果,倒计时
``` python
count = 0
b = 60
while (count < b):
count += 1
n = b - count
print n
time.sleep(1)
```
如果覆盖上一次print的结果
5374 次点击
所在节点    Python
7 条回复
ipconfiger
2015-05-29 17:24:36 +08:00
print “\b\b\b\b”
jedyu
2015-05-29 17:27:50 +08:00
\b 回到行首
LeoQ
2015-05-29 17:40:41 +08:00
clear能用么
Sylv
2015-05-29 17:41:57 +08:00
import sys
import time

print '1',
sys.stdout.flush() # 输出缓存
time.sleep(1)
print '\r', # 回车符回到行首
print '2',
lzachilles
2015-05-29 17:44:39 +08:00
cnt = 1
while True:
cnt += 1
print cnt,
print '\r',
n37r06u3
2015-05-29 18:30:07 +08:00
回车符 就是换行 想想打字机
cc7756789
2015-05-31 09:29:22 +08:00
搞定但是先问题无法解决,就是倒计时输出会在最左边输出,如何把它禁锢在指定的区域呢?
```
def interface_show(**kwargs):
print ' '*5, kwargs['title'], kwargs['artist'], kwargs['rate'], time_remain(kwargs['minutes'])

def time_remain(mins):
count = 0
while (count < mins):
count += 1
n = mins - count
time.sleep(1)
sys.stdout.write("%d \r" % n,)
sys.stdout.flush()
if not n:
return 'completed'
```


最后输出这样的信息,最右边的是总时长,最左边的是倒计时效果。
108 <旅立ち(「はるかな旅」ストリングス·ヴァージョン)> 吉田潔 ★★★★ 205

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/194744

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX