2015-05-10 29 views
0

我想要改变一条线上的简单计数器。我有以下几点:如何在Python 2.7中打印当前行?

from __future__ import print_function 
from time import sleep 
for i in range(400): 
    print("\r" + str(i), end = "") 
    sleep(0.5) 

在Python 3,它做工精细,用计数器递增在同一行,但在Python 2,没有显示。我怎样才能让它在Python 2中显示?


编辑:我对那两个Python的工作,2和3

+0

作品只是保证(输出unbuffered)[http://stackoverflow.com/questions/107705/disable-output-buffering] – keety

回答

1

这应该做你想做的解决方案倾斜:对我来说

from sys import stdout 
from time import sleep 
for i in range(400): 
    print"\r"+str(i), 
    stdout.flush() 
    sleep(0.5) 
+0

这有点冗长,但感谢您的帮助! – d3pd