python - ANSI escape code wont work on python interpreter
问题描述
ANSI code wont work on my python interpreter
I wanted to color some of my prints on the project. I looked up on how to color printed characters and found the ANSI escape codes, so i tried it up on the interpreter, but it wont work.
for example:
print("\033[32m Hello")
it Gives me <-[32m Hello
(an arrow left sign).
how do i make it work? can it work on python's interpreter? if not, where should i use it?
解决方案
您最好安装其他软件包来帮助生成 ANSI 序列,iirc win32 控制台本身不支持ANSI 颜色。一种选择是安装colorama。以下代码段打印出红色文本。
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')
编辑:经过更多研究,我意识到 Windows 10支持ANSI Escape Sequence,您可以通过调用来实现。如果您打算复制和粘贴,请使用此选项。
import os
os.system("echo [31mThis is red[0m")
不过我还是更喜欢前者。
推荐阅读
- javascript - 如何从非字符串格式的 EJS 方法返回 Html
- flutter - 颤振构建apk中的错误
- sql - 仅限用户使用的 Oracle 会话参数
- android - 如何过滤与android room db的一对多关系
- ios - 更新 root.plist 中的值
- flutter - 我想单击文档图片并对其进行裁剪(如 cam 扫描仪),这将在用户单击我的 sliverList 时完成。我是新来的颤振
- reactjs - 在功能组件 ReactJS 上更新数组
- jekyll - 将 Markdown 导出到 GitHub Pages 时处理内部 git-repository 链接
- docker - Visual Studio docker-compose 正在覆盖容器 COMMAND
- google-analytics - 增强型电子商务列表归因不适用于 Measurement Protocol HTTP 请求