0

I am beginner in Python and I write a code which is looping and requesting html data from a webpage. Then I am printing that data. But Pycharm terminal print every time when it's get the data. So I think I can use time.sleep(5) function because I need a couple of seconds to read the output. But when the next output came, it's writing to below of the last output and my terminal goes down and down. So I think, if I can clear my terminal's output part in Pycharm, right before I print the results it would shown like it changes simultaneously. So how can I code that, is there any code or module to do that in Pycharm.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
GAThrawn
  • 5
  • 1

1 Answers1

0

If you are printing only one line of data, the easiest way to update the output is to use '\r', i.e. the carriage return character, to return to the start of the line.

Single line example:

import time

for x in range(1,10):
    print('Replace the number on a single line: {}\r'.format(x), end="")
    time.sleep(2)

However, if your data consist of multiple lines, you need a Python module such as curses.

Multiple lines example:

import curses
import time

if __name__ == "__main__":
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()

    try:
        for i in range(10):
            stdscr.addstr(0, 0, 'Replace the numbers on multiple lines')
            stdscr.addstr(1, 0, 'Line ONE:' + str(i))
            stdscr.addstr(2, 0, 'Line TWO:' + str(i))
            stdscr.refresh()
            time.sleep(0.5)
    finally:
        curses.echo()
        curses.nocbreak()
        curses.endwin()