我已经检查了可能的副本,但他们没有为我工作。
我要做的是旋转一个numpy数组,我希望在终端中看到这个动画:
if __name__ == "__main__":
o = np.ones((10,10))
while True:
for i in xrange(361):
sys.stdout.write(repr(rot_position(o, i)))
尝试打印后跟逗号,但它也不起作用。如何使它始终在同一行上打印(适用于python 2.7x)?
答案 0 :(得分:7)
你总是可以使用(在Python 3.x中):
print("sometext", end="")
python 2.7的任何内容?
将其添加到代码顶部:from __future__ import print_function
然后,您可以将print
用作函数 - from @rwilson's comment。
答案 1 :(得分:1)
python 2.x“没有换行符打印”是:print("sometext"),
但是,如果您尝试执行我认为您要执行的操作,请在while
和for
之间添加print("\r"),
这是一个“自动收报机”程序的例子,它说明了我在说什么。不同,但我认为它适用:
#!/usr/bin/python
import time
import sys
i = 0
hello = "hello world, goodbye universe "
while 1:
print("\r"),
cur = hello[i:] + hello[0:i]
print("%s" % cur),
sys.stdout.flush()
i += 1
if (i >= len(hello)):
i = 0
time.sleep(0.3)
答案 2 :(得分:0)
你可以用于python 2.7
print ("sometext", end="")
答案 3 :(得分:0)
删除并重新打印已编辑的行:
def clear_line():
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
然后你可以做类似的事情:
print("Installing... | ")
time.sleep(0.3)
clear_line()
print("Installing... / ")
time.sleep(0.3)
clear_line()
print("Installing... - ")
time.sleep(0.3)
clear_line()
print("Installing... \ ")
这将产生旋转动画。你可以用你的numpy数组做类似的事情。
另一个例子:
print("1")
print("2")
clear_line():
print("3")
哪个输出:
1
3