Python模块在Windows上启用ANSI for stdout? [英] Python module to enable ANSI for stdout on Windows?

查看:318
本文介绍了Python模块在Windows上启用ANSI for stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这意味着在导入模块之后,如果输出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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆