我想做几个语句,给出标准输出,而不看到换行之间的语句。
具体来说,假设我有:
for item in range(1,100):
print item
结果是:
1
2
3
4
.
.
.
如何让它看起来像:
1 2 3 4 5 ...
更好的是,是否可以将单个数字打印在最后一个数字之上,这样一次只有一个数字出现在屏幕上?
我想做几个语句,给出标准输出,而不看到换行之间的语句。
具体来说,假设我有:
for item in range(1,100):
print item
结果是:
1
2
3
4
.
.
.
如何让它看起来像:
1 2 3 4 5 ...
更好的是,是否可以将单个数字打印在最后一个数字之上,这样一次只有一个数字出现在屏幕上?
当前回答
for item in range(1,100):
if item==99:
print(item,end='')
else:
print (item,end=',')
输出: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12、13、14、15、16、17、18、19日,20日,21日,22日,23日,24日,25日,26日,27日,28日,29、30、31、32、33、34、35、36、37、38、39、40、41、42、43、44、45、46岁,47岁,48岁,49岁,50岁,51岁,52岁,53岁,54岁,55岁,56岁,57岁,今年58岁,59岁,60岁,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99
其他回答
为了让数字相互覆盖,你可以这样做:
for i in range(1,100):
print "\r",i,
只要把数字打印在第一列,这就可以工作。
编辑: 这里有一个版本,即使没有打印在第一列,也可以工作。
prev_digits = -1
for i in range(0,1000):
print("%s%d" % ("\b"*(prev_digits + 1), i)),
prev_digits = len(str(i))
我应该指出,这段代码经过测试,在Windows上的Python 2.5中,在Windows控制台中工作得很好。根据其他一些方法,可能需要刷新stdout才能查看结果。YMMV。
这么多复杂的答案。如果你使用的是python3,只需在打印的开头放置\r,并添加end= ", flush=True:
import time
for i in range(10):
print(f'\r{i} foo bar', end='', flush=True)
time.sleep(0.5)
这将在原地写入0 foo bar,然后是1 foo bar等。
在Python 3中,你可以这样做:
for item in range(1,10):
print(item, end =" ")
输出:
1 2 3 4 5 6 7 8 9
Tuple:你可以对Tuple做同样的事情:
tup = (1,2,3,4,5)
for n in tup:
print(n, end = " - ")
输出:
1 - 2 - 3 - 4 - 5 -
另一个例子:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
for item in list_of_tuples:
print(item)
输出:
(1, 2)
('A', 'B')
(3, 4)
('Cat', 'Dog')
你甚至可以像这样解包你的元组:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
# Tuple unpacking so that you can deal with elements inside of the tuple individually
for (item1, item2) in list_of_tuples:
print(item1, item2)
输出:
1 2
A B
3 4
Cat Dog
另一个变化:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
for (item1, item2) in list_of_tuples:
print(item1)
print(item2)
print('\n')
输出:
1
2
A
B
3
4
Cat
Dog
或者更简单:
import time
a = 0
while True:
print (a, end="\r")
a += 1
time.sleep(0.1)
End ="\r"将覆盖第一次打印的开头[0:]。
“顺便说一下......如何刷新它,所以它打印mi在一个地方,只要改变数字。”
这真的是个棘手的话题。zack所建议的(输出控制台控制代码)是实现这一目标的一种方法。
你可以使用(n)诅咒,但这主要适用于*nix。
在Windows上(这里有有趣的部分),这很少被提及(我不明白为什么),你可以使用Python绑定到WinAPI (http://sourceforge.net/projects/pywin32/默认也带有ActivePython) -这并不难,而且工作得很好。这里有一个小例子:
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
output_handle.WriteConsoleOutputCharacter( i, pos )
time.sleep( 1 )
或者,如果你想使用print(语句或函数,没有区别):
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
print i
output_handle.SetConsoleCursorPosition( pos )
time.sleep( 1 )
Win32console模块可以让你用Windows控制台做更多有趣的事情…我不是WinAPI的忠实粉丝,但最近我意识到我对它的反感至少有一半是由用C语言编写WinAPI代码引起的——python绑定更容易使用。
当然,所有其他的答案都很棒,而且很深奥,但是……如果我想打印上一行呢?或者写多行文本,而不是清除它,再写相同的行?我的解决方案使之成为可能。