Python模块在Windows上启用ANSI for stdout? [英] Python module to enable ANSI for stdout on Windows?
问题描述
这意味着在导入模块之后,如果输出ANSI转义字符串,它们将会。
有两个python模块能够做这个 colorama
和 tendo.ansiterm
模块,它最初是为 waf
。
通过初始测试表明 colorama
更成熟,即使它需要两行代码,而不是一行。
import sys
try:
import colorama
colorama.init()
except:
try:
import tendo.ansiterm
except:
pass
sys.stdout.write\033 [33mYellow Submarine]
sys.stderr.write\ 033 [31mred,red,wine!
现在,两者都将正常工作,但如果您尝试重定向只有一个stderr或stdout,ansiterm将输出ANSI代码以屏幕和重定向输出。
我不确定,但我怀疑正确的行为是剥离ANSI代码,当输出si不是tty,您不希望在日志文件中看到ANSI转义。
I am looking for a Python module that would add ANSI support under Windows.
This means that after importing the module, if you output ANSI escaped strings, they will appear accordingly.
There are two python modules that are able to do this colorama
and tendo.ansiterm
module, which was originally written for waf
.
By initial tests indicate that colorama
is more mature, even if it requires two lines of code instead of one.
import sys
try:
import colorama
colorama.init()
except:
try:
import tendo.ansiterm
except:
pass
sys.stdout.write"\033[33mYellow Submarine"
sys.stderr.write"\033[31mred, red , wine!"
Now, both will work normally but if you try to redirect only one of the stderr or stdout, ansiterm will output ANSI codes to screen and redirected output.
I'm not sure but I suspect that the correct behavior is to strip ANSI codes when the output si not a tty, you don't want to see ANSI escapes in log files.
这篇关于Python模块在Windows上启用ANSI for stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!