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
reaCodes
V2EX  ›  Python

Python 输出多行时如何原地刷新?

  •  
  •   reaCodes ·
    zhatlas · 2018-07-20 10:40:54 +08:00 · 10974 次点击
    这是一个创建于 2079 天前的主题,其中的信息可能已经有所发展或是发生改变。
    单行原地刷新方法挺多的,多行原地刷新要这么做?

    类似这样的多行
    +--------+------------------------------+------------+------------------+
    | 代码 | 名称 | 估算涨幅 | 更新时间 |
    |--------+------------------------------+------------+------------------|
    | 110022 | 易方达消费行业 | -0.61% | 2018-07-20 10:39 |
    | 260108 | 景顺长城新兴成长混合 | -0.03% | 2018-07-20 10:39 |
    | 000220 | 富国医疗保健行业混合 | +0.19% | 2018-07-20 10:39 |
    | 003095 | 中欧医疗健康混合 A | +0.07% | 2018-07-20 10:39 |
    | 161725 | 招商中证白酒指数分级 | -0.44% | 2018-07-20 10:39 |
    | 160222 | 国泰国证食品饮料行业指数分级 | -0.20% | 2018-07-20 10:39 |
    | 270002 | 广发稳健增长 | +0.08% | 2018-07-20 10:39 |
    | 519690 | 交银稳健配置混合 A | -0.04% | 2018-07-20 10:38 |
    | 270042 | 广发纳斯达克 100 指数 | -0.48% | 2018-07-20 04:00 |
    | 001092 | 广发生物科技指数(QDII) | -0.48% | 2018-07-20 04:00 |
    | 160213 | 国泰纳斯达克 100 指数 | -0.45% | 2018-07-20 04:00 |
    | 000369 | 广发全球医疗保健现钞 | -0.34% | 2018-07-20 04:00 |
    +--------+------------------------------+------------+------------------+
    24 条回复    2018-07-20 18:54:54 +08:00
    wizardoz
        1
    wizardoz  
       2018-07-20 11:05:06 +08:00   ❤️ 1
    xiexingjia
        2
    xiexingjia  
       2018-07-20 11:12:42 +08:00   ❤️ 1
    import sys
    import time

    for i in range(10):
    print('\r{}'.format(i), end='')
    sys.stdout.flush()
    time.sleep(0.1)
    print()
    xiexingjia
        3
    xiexingjia  
       2018-07-20 11:14:05 +08:00   ❤️ 1
    import sys
    import time

    for i in range(10):
    [缩进] print('\r{}'.format(i), end='')
    [缩进] sys.stdout.flush()
    [缩进] time.sleep(0.1)
    print()
    qsnow6
        4
    qsnow6  
       2018-07-20 11:14:39 +08:00
    清屏后再打印就可以了
    reaCodes
        5
    reaCodes  
    OP
       2018-07-20 11:26:15 +08:00
    @wizardoz 我用的 Windows 系统,貌似没有这个模块吧
    reaCodes
        6
    reaCodes  
    OP
       2018-07-20 11:30:17 +08:00
    @qsnow6 清屏在打印屏幕容易闪烁,能不能直接覆盖?
    qsnow6
        7
    qsnow6  
       2018-07-20 11:34:17 +08:00
    @reaCodes 可以看看这个项目 https://www.v2ex.com/t/318369
    zhangpeter
        8
    zhangpeter  
       2018-07-20 11:40:12 +08:00
    print('/r666')
    whoami9894
        9
    whoami9894  
       2018-07-20 12:04:32 +08:00 via Android
    多行刷新 windows 下暂时不知道怎么实现

    单行的话 windows 下#3 那个方法 line5 的 print 函数换成 sys.stdout.write 就可以了
    reaCodes
        10
    reaCodes  
    OP
       2018-07-20 12:23:03 +08:00
    @qsnow6 这个项目貌似也不支持 Windows
    0312birdzhang
        11
    0312birdzhang  
       2018-07-20 12:39:16 +08:00
    `\r`了解一下?
    scalaer
        12
    scalaer  
       2018-07-20 12:43:49 +08:00
    退格键了解下
    scalaer
        13
    scalaer  
       2018-07-20 12:44:34 +08:00
    \x08
    reaCodes
        14
    reaCodes  
    OP
       2018-07-20 13:15:57 +08:00
    @mggis0or1 那效果跟清屏命令类似吧,也会出现闪烁,不是覆盖的那种效果
    reaCodes
        15
    reaCodes  
    OP
       2018-07-20 13:16:29 +08:00
    reaCodes
        16
    reaCodes  
    OP
       2018-07-20 13:21:46 +08:00
    @0312birdzhang 这个只能跳到最后一行的开后,不能跳到第一行的开头,对多行不起作用
    ysc3839
        17
    ysc3839  
       2018-07-20 13:28:56 +08:00 via Android
    @reaCodes Windows 的话有 API 设置控制台光标位置,设置后再 print 就可以覆盖了。
    0312birdzhang
        18
    0312birdzhang  
       2018-07-20 13:56:06 +08:00
    @reaCodes #16 试试这个

    ```
    import time
    import sys
    count = 1
    while count < 99:
    sys.stdout.write("current {0}%\r".format(count))
    sys.stdout.flush()
    count += 1
    time.sleep(0.5)
    ```
    yuyang
        19
    yuyang  
       2018-07-20 14:23:42 +08:00
    https://github.com/Yinzo/reprint 应该是符合你的要求的,配合 prettytable 搞一搞,linux 没问题,windows 不知道
    NxnXgpuPSfsIT
        20
    NxnXgpuPSfsIT  
       2018-07-20 16:16:46 +08:00
    @reaCodes Windows 下面我见过两种比较简单的解决方案
    gist.github.com/littlecodersh/2ab4d88070eb16bf9f2b26f7cbc16ad0
    araraloren
        21
    araraloren  
       2018-07-20 16:44:19 +08:00
    库 ncursers ?
    JCZ2MkKb5S8ZX9pq
        22
    JCZ2MkKb5S8ZX9pq  
       2018-07-20 17:43:33 +08:00
    搭车问一句,mac 下中英文混合的这种表格,比如 prettytable,你们怎么搞对齐的?
    coderbean
        23
    coderbean  
       2018-07-20 17:58:10 +08:00 via iPhone
    akira
        24
    akira  
       2018-07-20 18:54:54 +08:00   ❤️ 1
    ansi 控制码,VT100 了解下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2888 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:57 · PVG 21:57 · LAX 06:57 · JFK 09:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.