0

I need to print 'ok' in same place. Any ways to do it?

I've found solutions but they don't works with IDLE correctly:

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

I want every 'ok' on the same line. Thanks!

Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
Gerswin Lee
  • 1,265
  • 2
  • 11
  • 12

4 Answers4

3

Something like this?

Code:

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

Or, if your 'ok' is at the beginning of the line you can use a single '\r' instead:

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

Output:

ok
Matt
  • 3,651
  • 3
  • 16
  • 35
  • 1
    And in general, '\r' will back up to the beginning of the line. – chepner Feb 09 '13 at 16:18
  • @GerswinLee Looks like windows terminals don't like the backspace character. You could try `\r` instead, but I suspect the result will be the same. – Matt Feb 09 '13 at 16:20
  • @GerswinLee [this question](http://stackoverflow.com/questions/9667462/backspace-behavior-in-python-statement-what-is-correct-behavior-of-printing-a) seems to suggest that backspace should work in windows terminals. Can you replicate the results shown there? – Matt Feb 09 '13 at 16:32
2

For example, if you want to print "OK" in the exact same spot, you can do :

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

Result :

OK

worte 10000 times on the same console spot.

dymayday
  • 36
  • 1
  • 3
0

That ? :

from sys import stdout

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

EDIT

I think it isn't possible to do just one OK, in IDLE. But the following code will give idea of what is possible in a console:

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')

result

OKOKOKOKOKOKOKOK
eyquem
  • 26,771
  • 7
  • 38
  • 46
0

If you need to print them one at a time, use one of the other answers. This will print them all at once:

my_var = ''
while (count < 9):
      if statusm == "<Status>OK</Status>":
         my_var += 'ok'
print my_var
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94