【发布时间】:2016-10-23 21:47:50
【问题描述】:
我对 Python 很陌生,但我正在尝试做一些我认为非常简单的事情。然而,它已经困扰了我很长一段时间了。
我有一个执行一定次数迭代的代码,我想在同一行打印进度状态。
我尝试了以下
y=1000000
for x in range(y):
if x % 100000 == 0 and x!=0 or x==y :
print " Iteration %d out of %d\r" % (x,y)
但我得到的不是回车而是简单的
Iteration 100000 out of 1000000
Iteration 200000 out of 1000000
Iteration 300000 out of 1000000
Iteration 400000 out of 1000000
Iteration 500000 out of 1000000
...
在视频中打印出来。
有趣的是,如果我这样做了
for x in range(1000000):
print "%d\r" % x,
它完成了这项工作。 有人知道为什么吗?
【问题讨论】:
-
print自动添加换行符。