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

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

问题描述

如果我在 linux 下的 python 中运行它,它可以工作:

If I run this in python under linux it works:

start = "33[1;31m"
end = "33[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?

推荐答案

对于 windows,调用 os.system("color") 使 ANSI 转义序列得到正确处理:

For windows, calling os.system("color") makes the ANSI escape sequence get processed correctly:

import os
os.system("color")

COLOR = {
    "HEADER": "33[95m",
    "BLUE": "33[94m",
    "GREEN": "33[92m",
    "RED": "33[91m",
    "ENDC": "33[0m",
}

print(COLOR["GREEN"], "Testing Green!!", COLOR["ENDC"])

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

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