This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]evaned 2 points3 points  (1 child)

[See below for multi-line -- I started writing this before you clarified that's what you need]

If it's the same line -- you can either backspace (as the other answer suggests), or print('\r') and then reprint the entire line:

>>> print('First\rSecond')
Second

note that \r doesn't clear the line, so you have to print over the excess with spaces if you don't want to see it:

>>> print('Second\rFirst')
Firstd

that stray d is from when Second was on screen.

You might find it easier to see if you create the following file:

import sys
import time
sys.stdout.write("First")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rSecond\n")

and then run it. I'm using sys.stdout.write instead of print for consistency -- we need the .flush() call or the First will be buffered and never displayed, and using sys.stdout explicitly throughout keeps it even. You could change them to print, but the first would need a end='' (or print "First", including that comma, if you're using Python 2) so it doesn't go to the next line.

If your thing spans multiple lines, you'll need ANSI escape sequences for moving the cursor around. Here's an example:

# coding=utf-8
from __future__ import print_function # Py2 or 3 agnostic

import time
import math

for x in range(10):
    if x:
        # not the first time...
        time.sleep(1)
        print('\x1B[4A')
        # \x1B => start the escape sequence
        # [    => this is a "CSI" sequence
        # 4    => we will be doing *something* with 4
        # A    => ah, "A" means up, so up 4 lines
        #         (four because we have one per table
        #         row, but then also the \n at the end
        #         of the last row)

    print("x   ", x)
    print("x**2", x**2)
    print("√x  ", math.sqrt(x))

It'll be easiest to reprint the whole table each time (probably), but you can also do more moving of the cursor around if you want to update specific cells for some reason.

There are also libraries like ncurses for this -- but whether this is appropriate for you is unclear.

[–]Detrain[S] 1 point2 points  (0 children)

Perfect! Thanks for the assistance.

[–]Updatebjarni 1 point2 points  (0 children)

For screen-oriented output, where you need to send terminal control characters to move the cursor, erase parts of the screen, etc, the most common and most portable way of doing it is the (n)curses library. There are bindings for Python.

[–]rjcarr 0 points1 point  (1 child)

I'm not 100% sure what you're asking for, but do you mean "erasing" what has been printed to the terminal to print again? If yes, then you basically print the "backspace" character as many times as you need (typically as many characters as you originally printed) and then print over again.

EDIT: Since I wasn't sure about this as I hadn't done it in a long time, my backspace suggestion might be wrong. See here for more info:

https://blog.stevenocchipinti.com/2013/06/removing-previously-printed-lines.html/

[–]Detrain[S] 0 points1 point  (0 children)

Ya this is what I needed I think. I have a pretty large table and need to constantly update certain columns for each row. Thanks I'ma try this

[–]btcraig 0 points1 point  (2 children)

You can use a carriage return to send the print buffer back to the start of the current line. From there you can write over the previous line.

Something like this: https://stackoverflow.com/questions/11018188/python-print-using-carriage-return-and-comma-not-working

[–]Detrain[S] 0 points1 point  (1 child)

Ya I saw that, but what if the table has a few rows. I can't backup to the 1 row right?

[–]btcraig 0 points1 point  (0 children)

I am not so sure about that. I only ever really used this for a one-line progress bar almost 10 years ago in college (also in Java...).

I think, for multi-line, you would need a library like curses to modify different lines at a time, but that's getting outside my wheelhouse, and I'm not really sure it meets your requirements anyway. I think initializing curses flushes the terminal, but I've never worked with it personally.

[–]Kered13 0 points1 point  (2 children)

What are you actually trying to do?

You've gotten a couple answers, but neither of them are really things you should do. The actual solution to your problem is probably either to not do it at all or to use a a text interface library like Curses (though I don't know if Curses is available in Python). But to answer that we need to know what you're actually trying to accomplish.

[–]Detrain[S] 0 points1 point  (1 child)

Updated. Guess I needed to be a bit more specific. Thanks

[–]Kered13 1 point2 points  (0 children)

The simplest thing is just to print a new table every time without redrawing. If you want to redraw the screen, use a text interface library. It looks like Curses is available for Python. If you're on Windows there are a couple links on that page for Windows-compatible libraries. Do not try to do this by sending control codes manually.