Python - removing and replacing printable elements

[Using Python 3.2] I was wondering if it is possible to remove the elements that you typed in python, not from python gui, but from the propt command. eg.

a = 0 for x in range (0,3): a = a + 1 b = ("Loading" + "." * a) print (a) 

so that he prints

 >>>Loading >>>Loading. >>>Loading.. >>>Loading... 

But, My problem is that I want all this to be on the same line, and for this she removes it when something else arrives. therefore, instead of going, “download”, “download”. “loa .... I want him to get“ Download. ”Then he removes what is on the line and replaces it with“ Download .. ”and then removes“ Download .. ”and replaces it (in the same line) to “Loading ...” It’s hard to describe.

ps I tried to use the Backspace pattern, but it does not work ("\ b")

thank

+35
python printing
Mar 13 '11 at 17:15
source share
3 answers

Just use CR to go to the beginning of the line.

 import time for x in range (0,5): b = "Loading" + "." * x print (b, end="\r") time.sleep(1) 
+65
Mar 13 '11 at 18:20
source share

One way is to use ANSI escape sequences :

 import sys import time for i in range(10): print("Loading" + "." * i) sys.stdout.write("\033[F") # Cursor up one line time.sleep(1) 

Also sometimes useful (for example, if you print something shorter than before):

 sys.stdout.write("\033[K") # Clear to the end of line 
+58
Mar 13 2018-11-11T00:
source share
 import sys import time a = 0 for x in range (0,3): a = a + 1 b = ("Loading" + "." * a) # \r prints a carriage return first, so `b` is printed on top of the previous line. sys.stdout.write('\r'+b) time.sleep(0.5) print (a) 

Please note that you may need to run sys.stdout.flush() immediately after sys.stdout.write('\r'+b) , depending on which console you are printing so that the results are printed on demand without which any buffering.

+9
Mar 13 '11 at 18:05
source share



All Articles