Python中文
首页
教程
问答
标签
搜索
登录
注册
在python中使用print刷新stdout行时出现问题
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我一直在尝试使用以下代码打印python2.7中for循环的进度:</p> <pre><code>for i in range(100): if float(i) % 10.0 == 0: print i, "\r", </code></pre> <p>我所追求的行为是在std-out上刷新同一行,而不是每次都写一行新的行。在</p> <p>编辑1:</p> <p>在我的控制台(Xfce终端0.4.8)中进行测试时,实际上无论是否包含<code>if</code>语句,我都没有得到任何输出。在</p> <p><strong>为什么没有输出?</strong></p> <p>我最初说stdout的行为取决于if语句是否存在,因为我将产生问题的代码简化为最简单的形式(只为了产生上述效果)。我很抱歉。在</p> <p>编辑2:</p> <p>多亏了<a href="https://stackoverflow.com/a/10552693/1358677">senderle</a>,这个问题就解决了。如果您错过了<code>sleep()</code>命令,那么打印和回车会很快发生,您看不到它们。在</p> <p>编辑3:</p> <p>最后一件事。如果您没有捕捉到<code>range(100)</code>中的最后一个数字,即99,那么这个数字将从屏幕上清除。在</p> <p>编辑4:</p> <p>注意<a href="https://stackoverflow.com/a/10552693/1358677">senderle</a>答案中<code>print i</code>后的逗号。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>按原样测试代码,并在第一行末尾包含一个:冒号:,对于32位的Py2.7和64位的Windows7,都能正常工作。在</p> <p>在您的<code>if</code>或<code>for</code>块中是否有可能导致新行被写出的输出写入?在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
5 回答
如何在乒乓球游戏中阻止球
7 回答
如何在乘法和模中不乘空间?
1 回答
如何在乘法和除以2个不同的数字之间进行交换?
4 回答
如何在也是数据一部分的单个字符上拆分大字符串
3 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
7 回答
如何在事件wxWidgets中传递自定义数据
8 回答
如何在事件中使用lambda i=i?
7 回答
如何在事件中心只接收最近的数据
1 回答
如何在事件发生之前保持云函数运行?
10 回答
如何在事件发生后使页面重定向到同一页面
6 回答
如何在事件回调之间保持python生成器的状态
6 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
6 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
5 回答
如何在事件处理程序中访问外部对象?
1 回答
如何在事件循环中将协程打包为正常函数?
5 回答
如何在事件循环之外运行协同程序?
2 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
1 回答
如何在事件文件中只保留一份摘要?
2 回答
如何在事件模板中添加事件
2 回答