Frage

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?

War es hilfreich?

Lösung

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"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top