简体   繁体   中英

Use \r on a String with multiple \n

I have a String with multiple \\n in it to get it in a square form f.ex. for a matrix, a map etc.

If I want to override this with

sys.stdout.write("\\r %s" % TheString) sys.stdout.flush()

it jumps to the beginning of the last row, overrides it and prints the new string underneath. But I want it to jump all the way up to the first row and override the entire output. I tried it with multiple \\r (times number of rows) but it didn't work. Is there an other possibility?

Thanks!

IIUC it's not easily possible as pointed by @Boldewyn but you may clear the screen from the previous outputs before issuing a new sys.stdout.write using os.system("clear") (in UNIX-like systems):

import os
import sys

sys.stdout.write("This")
sys.stdout.write("Will")
sys.stdout.write("Not")
sys.stdout.write("Show")

# After this command all output before will be cleared
os.system("clear")
sys.stdout.write("\r %s" % "Blablablabla")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM