1

I want edit some(More than one line) of the last lines in output. But I have a lot of lines before them that clearing and reprinting all them slows down my program (I can't use '\r' Because the lines I want to edit are more than one line).

print("Lots of lines")
print("Lines to be edited\nLines to be edited")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
naser
  • 33
  • 5
  • 1
    Even if you did want to clear the screen completely, `os.system('cls')` is a bad idea -- it's very slow to start a whole separate program to do nothing but send some terminal control characters (the exact same ones every time!) your program could just send itself. – Charles Duffy Nov 19 '20 at 16:41
  • Just on Windows ? – RashidLadj_Winux Nov 19 '20 at 16:43
  • you can maybe use sys.stdout.write(), for better infos search on the net – Leonardo Scotti Nov 19 '20 at 16:44
  • (Which terminal(s) you need to support matters; if you want something to run on UNIX-y platforms you'll want to use a library that will check the `TERM` environment variable, look up the right control codes for whichever terminal is in use, etc). – Charles Duffy Nov 19 '20 at 16:45
  • @LeonardoScotti, ..the hard part isn't knowing how to write to stdout, it's knowing _what_ to write to stdout. – Charles Duffy Nov 19 '20 at 16:45
  • @naser, err, that question was "just on Windows" as in "Microsoft Windows?", not as in "one window or more than one?" – Charles Duffy Nov 19 '20 at 16:46
  • ...anyhow, if my very partial understanding of Windows is correct, its terminal uses ANSI control sequences. The documentation at http://ascii-table.com/ansi-escape-sequences.php tells you what to send for each operation (moving the cursor, clearing the line the cursor is currently on, etc). – Charles Duffy Nov 19 '20 at 16:47
  • Not sure, but you might look into the [curses](https://docs.python.org/3/library/curses.html) library. Maybe this can do what you want. – wovano Nov 19 '20 at 16:51
  • You may wanna check this: https://stackoverflow.com/questions/27612545/how-to-change-the-location-of-the-pointer-in-python – Himanshu Tanwar Nov 19 '20 at 16:52
  • I need a solution on windows platform – naser Nov 19 '20 at 16:52

1 Answers1

1

You can use:

print ("\033[A                             \033[A")

it clears one line from the output. Below you can see example of how it works:

print("Hello world\n xxx \n xxx")

print ("\033[A                             \033[A")
print ("\033[A                             \033[A")

Output:

Hello world

I wanted to clear two output lines containing xxx so I printed it twice.

You can play with it and make some for loops etc.

Please note that on Windows you may need to enable the ANSI support using the following http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html

"\033[A" string is interpreted by terminal as move the cursor one line up.

FilipA
  • 526
  • 3
  • 10
  • My terminal shows this after run this code: Hello world xxx xxx [A [A [A [A – naser Nov 19 '20 at 17:03
  • @naser maybe the issue is with your terminal. I have checked on Windows and also on my VM with Ubuntu and it also works fine. And even online compilers have no issue with it ( https://onlinegdb.com/S1i9rQ4cw and repl.it ) – FilipA Nov 19 '20 at 17:04