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.
Asked
Active
Viewed 733 times
0
-
1Here is the same question with answers: https://stackoverflow.com/a/47367480/13714686 – August Kimo Oct 25 '20 at 13:08
-
Output could be suspended in the Run tool window: https://www.jetbrains.com/help/pycharm/stopping-and-pausing-applications.html#suspend – user2235698 Oct 27 '20 at 16:02
1 Answers
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()

Silvio Gregorini
- 178
- 1
- 6