替换 Python 2.7 中的打印语句 [英] Replace printed statements in Python 2.7
本文介绍了替换 Python 2.7 中的打印语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何替换 Python 2.7 中打印的语句,例如:
for i in range(100):打印 i,"% Completed"#Result 必须是:>>>1% 已完成
1 必须替换为 2,依此类推.但不能在不同的行或附加在同一行.
我尝试搜索它,找到了诸如
之类的解决方案from __future__ import print_function打印(我:"+ str(i),结束=\ r")
但它们会导致附加的打印语句.我的 print_function
是否有任何更正或有不同的解决方案?
解决方案
你想要这样的东西:
<预><代码>>>>导入系统,时间>>>对于我在范围内(100):... time.sleep(0.1)... sys.stdout.write('%d %% Completed \r' % (i,))... sys.stdout.flush()How to replace printed statements in Python 2.7, for example:
for i in range(100):
print i,"% Completed"
#Result Must Be:
>>> 1 % Completed
The 1 must be replaced by 2 and so on. But not in different line or append on same line.
I tried searching about it, found solution such as
from __future__ import print_function
print("i: "+str(i),end="\r")
But they result in appended print statements. Is there any correction in my print_function
or is there a different solution?
解决方案
Do you want something like this:
>>> import sys, time
>>> for i in range(100):
... time.sleep(0.1)
... sys.stdout.write('%d %% Completed \r' % (i,))
... sys.stdout.flush()
这篇关于替换 Python 2.7 中的打印语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文