V
V
Vasily Nikonov2020-05-07 22:02:00
Python
Vasily Nikonov, 2020-05-07 22:02:00

How can I delete existing text from the console in Python 3.8?

I have a program that outputs to the console (macOS or Linux) the time in large characters (see attached screenshot). I want the program to simply start outputting the required characters again. But I'm not satisfied with the complete clearing of the console ('clear' or 'cls'). I want the program to just start outputting new data.

If so, the program outputs the characters line by line.

Here is the code itself:

#Необходимые модули.
from datetime import datetime #Модуль для определения времени.
from time import sleep #Модуль для 'простойки'.

#Все необходимые символы.
digits = [
[' ---- ','|    |','|    |','|    |','|    |','|    |',' ---- '], #Ноль.
['      ','     |','     |','     |','     |','     |','      '], #Один.
[' ---- ','     |','     |',' ---- ','|     ','|     ',' ---- '], #Два.
[' ---- ','     |','     |',' ---- ','     |','     |',' ---- '], #Три.
['      ','|    |','|    |',' ---- ','     |','     |','      '], #Четыре.
[' ---- ','|     ','|     ',' ---- ','     |','     |',' ---- '], #Пять.
[' ---- ','|     ','|     ',' ---- ','|    |','|    |',' ---- '], #Шесть.
[' ---- ','     |','     |','     |','     |','     |','      '], #Семь.
[' ---- ','|    |','|    |',' ---- ','|    |','|    |',' ---- '], #Восемь.
[' ---- ','|    |','|    |',' ---- ','     |','     |',' ---- '], #Девять.
['      ','      ','  []  ','      ','  []  ','      ','      ']] #Точки (разделители).

#Начинаем выводить символы в консоль.
for j in range(3):
    now = list(str(datetime.now())[11:19:1]) #Здесь мы находим реальное вермя ЧЧ:ММ:CC.
    for i in range(7):
        #Вывод построчно больших цифр.
        print ('{0} {1}{6}{2} {3}{6}{4} {5}'.format(digits[int(now[0])][i], digits[int(now[1])][i], digits[int(now[3])][i],
        digits[int(now[4])][i], digits[int(now[6])][i], digits[int(now[7])][i],digits[10][i]))
    sleep(1) #Ждём секунду.


If anything, then at the moment 'for j in range (3):' in the future will be rewritten to 'while True:'

5eb459c8c147e009218880.png

I need the previous characters (in this case, 21:55:47 to be overwritten and new ones to appear in their place) :55:48 and so on)

And if, of course, it is possible, then shorten the output of characters to the console a little (in the .format() method)

Help, good people! :O

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-05-07
@sanya84

To clear the console, you can use the cls command in Windows. It works.
I think it also exists in Linux .

#Необходимые модули.
import os
from datetime import datetime #Модуль для определения времени.
from time import sleep #Модуль для 'простойки'.

#Все необходимые символы.
digits = [
[' ---- ','|    |','|    |','|    |','|    |','|    |',' ---- '], #Ноль.
['      ','     |','     |','     |','     |','     |','      '], #Один.
[' ---- ','     |','     |',' ---- ','|     ','|     ',' ---- '], #Два.
[' ---- ','     |','     |',' ---- ','     |','     |',' ---- '], #Три.
['      ','|    |','|    |',' ---- ','     |','     |','      '], #Четыре.
[' ---- ','|     ','|     ',' ---- ','     |','     |',' ---- '], #Пять.
[' ---- ','|     ','|     ',' ---- ','|    |','|    |',' ---- '], #Шесть.
[' ---- ','     |','     |','     |','     |','     |','      '], #Семь.
[' ---- ','|    |','|    |',' ---- ','|    |','|    |',' ---- '], #Восемь.
[' ---- ','|    |','|    |',' ---- ','     |','     |',' ---- '], #Девять.
['      ','      ','  []  ','      ','  []  ','      ','      ']] #Точки (разделители).

#Начинаем выводить символы в консоль.
while True:
    now = list(str(datetime.now())[11:19:1]) #Здесь мы находим реальное вермя ЧЧ:ММ:CC.
    for i in range(7):
        #Вывод построчно больших цифр.
        print ('{0} {1}{6}{2} {3}{6}{4} {5}'.format(digits[int(now[0])][i], digits[int(now[1])][i], digits[int(now[3])][i],
        digits[int(now[4])][i], digits[int(now[6])][i], digits[int(now[7])][i],digits[10][i]))
    sleep(1) #Ждём секунду.
    os.system("cls")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question