Python:如何使ANSI转义码在Windows中也能工作? [英] Python: How can I make the ANSI escape codes to work also in Windows?

查看:111
本文介绍了Python:如何使ANSI转义码在Windows中也能工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Linux下的python中运行它,则可以正常运行:

If I run this in python under linux it works:

start = "\033[1;31m"
end = "\033[0;0m"
print "File is: " + start + "<placeholder>" + end

但是,如果我在Windows中运行它不起作用,我该如何制作ANSI转义码在Windows上也可以工作吗?

But if I run it in Windows it doesn't work, how can I make the ANSI escape codes work also on Windows?

推荐答案

您可以检查 Python模块为Windows上的stdout启用ANSI颜色?看看它是否有用。

You could check Python module to enable ANSI colors for stdout on Windows? to see if it's useful.

colorama 模块似乎是跨平台的。

The colorama module seems to be cross-platform.

您安装colorama:

You install colorama:

pip install colorama

然后:

import colorama
colorama.init()
start = "\033[1;31m"
end = "\033[0;0m"
print "File is: " + start + "<placeholder>" + end

这篇关于Python:如何使ANSI转义码在Windows中也能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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