V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
cc7756789
V2EX  ›  Python

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

  •  
  •   cc7756789 · 2015-05-29 17:17:45 +08:00 · 5365 次点击
    这是一个创建于 3252 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就比如像下面这种循环结果,倒计时
    ``` python
    count = 0
    b = 60
    while (count < b):
    count += 1
    n = b - count
    print n
    time.sleep(1)
    ```
    如果覆盖上一次print的结果
    7 条回复    2015-05-31 09:29:22 +08:00
    ipconfiger
        1
    ipconfiger  
       2015-05-29 17:24:36 +08:00
    print “\b\b\b\b”
    jedyu
        2
    jedyu  
       2015-05-29 17:27:50 +08:00
    \b 回到行首
    LeoQ
        3
    LeoQ  
       2015-05-29 17:40:41 +08:00 via Android
    clear能用么
    Sylv
        4
    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
        5
    lzachilles  
       2015-05-29 17:44:39 +08:00
    cnt = 1
    while True:
    cnt += 1
    print cnt,
    print '\r',
    n37r06u3
        6
    n37r06u3  
       2015-05-29 18:30:07 +08:00
    回车符 就是换行 想想打字机
    cc7756789
        7
    cc7756789  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:04 · PVG 21:04 · LAX 06:04 · JFK 09:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.