2013-02-09 78 views
0

我需要在相同的位置打印「ok」。任何方式來做到這一點?python打印一行相同的空間

我找到了解決方案,但他們沒有工作與IDLE正確:

while (count < 9): 
     if statusm == "<Status>OK</Status>": 
     print "ok", 

我想在同一行上每一個「OK」。謝謝!

+5

你是什麼意思在同一個地方? – 2013-02-09 16:06:21

+1

需要更多信息來回答這個問題。 「同樣的東西」是什麼意思,你在哪裏打印?一個文件,Windows終端? – Will 2013-02-09 16:07:26

+0

是的Windows終端 該代碼打印好吧好吧好吧好吧好吧沒有,打印好的新行和其他好吧我只是需要一個沒有更多 – 2013-02-09 16:09:14

回答

1

例如,如果你想在完全相同的點打印 「OK」,你可以這樣做:

for i in range(0, 10000): 
    print("\rOK", end='') 

結果:

OK 

在相同的控制檯點上10000次worte。

+0

似乎無法在Python 2.7 + IPython上工作。我需要改變什麼? – rohitkulky 2015-04-16 10:08:31

3

是這樣的嗎?

代碼:

for i in range(5): 
    print '\b\b\bok', 

或者,如果你的 'OK' 是你可以使用一個單一的 '\ r',而不是行的開頭:

for i in range(5): 
    print '\rok', 

輸出:

ok 
+1

一般而言,'\ r'會回到行首。 – chepner 2013-02-09 16:18:49

+0

http://sms.gerswin.com/okprint.png 不工作 – 2013-02-09 16:19:05

+0

@GerswinLee看起來像Windows終端不喜歡退格字符。你可以嘗試使用'\ r',但我懷疑結果是一樣的。 – Matt 2013-02-09 16:20:07

0

是什麼? :

from sys import stdout 

count = 1  
statusm = "<Status>OK</Status>" 
while (count < 9): 
     if statusm == "<Status>OK</Status>": 
      stdout.write('OK') 
      count += 1 

編輯

我認爲這是不可能做到這一個OK,在空閒狀態。但是,下面的代碼會給出什麼是可能在控制檯的想法:

from sys import stdout 
from time import sleep 

several = ("<Status>OK</Status>", 
      "<Status>bad</Status>", 
      "<Status>OK</Status>", 
      "<Status>bad</Status>", 
      "<Status>yes</Status>", 
      "<Status>OK</Status>", 
      "<Status>none</Status>", 
      "<Status>bad</Status>", 
      "<Status>OK</Status>", 
      "<Status>yes</Status>") 

good = ('OK','yes') 

for i,status in enumerate(several): 
    y = str(i) 
    stdout.write(y) 
    stdout.write(' OK' if any(x in status for x in good) else ' --') 
    sleep(1.0) 
    stdout.write('\b \b\b \b\b \b') 
    for c in y: stdout.write('\b \b') 

結果

OKOKOKOKOKOKOKOK 
+0

只是一個好,也許是不可能的 – 2013-02-09 16:33:17

0

如果需要打印它們一次一個,可以使用其他的答案之一。 這將一次全部打印出來:

my_var = '' 
while (count < 9): 
     if statusm == "<Status>OK</Status>": 
     my_var += 'ok' 
print my_var