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

关于 print 不换行

  •  
  •   hhrmatata · 2014-07-09 16:31:17 +08:00 · 5531 次点击
    这是一个创建于 3551 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python 2.7

    想将两次输出输出到同一行上,就在print 后面加了逗号,例如:

    import time

    print 'hehe ...',
    time.sleep(5)
    print 'finish!'

    但是有个问题,观察终端中的输出,发现观察到的程序的执行顺序是
    1. time.sleep(5)
    2. print 'hehe ...',
    3. print 'finish!'

    如何做到,先输出第一个print,再睡眠,最后输出第二个输出?两个输出在同一行。
    第 1 条附言  ·  2014-07-10 07:20:30 +08:00
    已解决。
    最后定义了这样一个函数就好了:

    def p(s):
    sys.stdout.write(s)
    sys.stdout.flush()
    return None
    14 条回复    2014-07-11 01:29:58 +08:00
    cj1324
        1
    cj1324  
       2014-07-09 16:36:19 +08:00   ❤️ 1
    直接用import sys; sys.stdout ?
    Ansen
        3
    Ansen  
       2014-07-09 16:41:37 +08:00   ❤️ 1
    为啥 我这里是按顺序执行的
    hhrmatata
        4
    hhrmatata  
    OP
       2014-07-09 16:41:43 +08:00
    @cj1324
    @pinepara
    谢谢

    最后定义了这样一个函数就好了:

    def p(s):
    sys.stdout.write(s)
    sys.stdout.flush()
    return None
    cj1324
        6
    cj1324  
       2014-07-09 16:44:15 +08:00   ❤️ 1
    @hhrmatata
    return None 不就是 return ? 这样写反而更让人疑惑。
    wy315700
        7
    wy315700  
       2014-07-09 16:46:00 +08:00   ❤️ 1
    终端的输出刷新是按行来的,除非手动调用flush
    mengzhuo
        8
    mengzhuo  
       2014-07-09 16:46:40 +08:00   ❤️ 1
    @hhrmatata 不要观察执行顺序,ipdb是你debug的好伙伴
    shyrock
        9
    shyrock  
       2014-07-09 16:47:45 +08:00   ❤️ 1
    python2.7.6 执行是对的啊。。。
    hehe ... finish!
    [Finished in 5.1s]
    Ever
        10
    Ever  
       2014-07-09 16:49:07 +08:00   ❤️ 1
    不用return就是return None

    python3可以print(word, end="")

    python2.7可以from __future__ import print_function后照python3的做
    Cynic222
        11
    Cynic222  
       2014-07-09 16:51:17 +08:00   ❤️ 1
    @cj1324
    不写return不就是return None?这样写反而更让人疑惑。-- 开个玩笑
    cakegg
        12
    cakegg  
       2014-07-09 19:38:33 +08:00   ❤️ 1
    import sys
    import time
    print "hello",
    stdout_temp = sys.stdout
    time.sleep(5)
    sys.stdout = stdout_temp
    print "finish"

    其实像上面这样写就可以了...
    hhrmatata
        13
    hhrmatata  
    OP
       2014-07-10 07:19:06 +08:00 via Android
    @cj1324
    @Cynic222
    个人习惯问题,方便阅读。明确告诉阅读代码的人是返回空而不是忘记写了。
    ggarlic
        14
    ggarlic  
       2014-07-11 01:29:58 +08:00   ❤️ 1
    这,,,,完全不用改代码啊
    python -u xx.py
    让python运行在unbuffered的模式不就行了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5315 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 09:06 · PVG 17:06 · LAX 02:06 · JFK 05:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.