The carriage return \r
in the string causes the cursor to be placed at the start of the line, so the ]
overwrites [
.
Change it to this:
OUTPUT = "[ \033[1m\x1b[32m" + DATE + "\x1b[0m ]\r"
質問
My goal is to visualize, for example, this output:
[ Sun, 09 06 2013 12:51:49 ]
The entire date must be colorized with this ANSI code:
\033[1m\x1b[32m DATE \x1b[0m\r
I tried with this piece of code:
import time
DATE = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
OUTPUT = "[ " + "\033[1m\x1b[32m" + DATE + "\x1b[0m\r" + " ]"
print OUTPUT
But it returns me a string completely wrong:
What's wrong with this?
解決
The carriage return \r
in the string causes the cursor to be placed at the start of the line, so the ]
overwrites [
.
Change it to this:
OUTPUT = "[ \033[1m\x1b[32m" + DATE + "\x1b[0m ]\r"