Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python overwrite multiline text

import sys
import time


def clear_to_start(text):
    lines = text.split('
') # separate lines
    lines = lines[::-1] # reverse list
    nlines = len(lines) # number of lines

    for i, line in enumerate(lines): # iterate through lines from last to first
        sys.stdout.write('
') # move to beginning of line
        sys.stdout.write(' ' * len(line)) # replace text with spaces (thus overwriting it)

        if i < nlines - 1: # not first line of text
            sys.stdout.write('x1b[1A') # move up one line

    sys.stdout.write('
') # move to beginning of line again


text = '''
this is my
multiline text
'''

text2 = '''
this is
multiline
'''

sys.stdout.write(text) # print text
sys.stdout.flush()
time.sleep(3) # sleep 3 seconds
clear_to_start(text) # clear lines and ascend to top
sys.stdout.write(text2) # overwrite text
Comment

PREVIOUS NEXT
Code Example
Python :: python logging repeated messages 
Python :: start ipython with any version 
Python :: geopandas dataframe to ogr layer 
Python :: filtering certain rows in python that contains a part of string 
Python :: Finding Maximum Elements along columns using Python numpy.argmax() 
Python :: update dataframe based on value from another dataframe 
Python :: open python file with read write permissions 
Python :: assignment 6.5 python for everybody 
Python :: make array consecutive 2 python 
Python :: normalized histogram pandas 
Python :: rename files in python 
Python :: python prevent print output 
Python :: export list to a file python 
Python :: case python 
Python :: print value of array python 
Python :: node 14 alpine add python 
Python :: install python to linux 
Python :: list addition within a list comprehension 
Python :: .dropna() python 
Python :: txt to image python 
Python :: os module 
Python :: migrations.RunPython 
Python :: type() function in python 
Python :: python divide and round away operator 
Python :: Sum of all substrings of a number 
Python :: or en python 
Python :: python find oldest and newest date 
Python :: relative frequency histogram python 
Python :: remove duplicates from list python keep order 
Python :: NumPy roll Example 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =