문제

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?

도움이 되었습니까?

해결책

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top