Question

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:

enter image description here

What's wrong with this?

Was it helpful?

Solution

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top