【问题标题】:is it possible to add colors to python output? [duplicate]是否可以为 python 输出添加颜色? [复制]
【发布时间】:2014-01-18 18:30:49
【问题描述】:

所以我为我、我的朋友和家人制作了一个小型密码强度测试器,如下所示:

import re
strength = ['You didnt type anything','Terrible','weak sause','avarage','Good!','Very Strong', 'THE FORCE IS STRONG WITH THIS ONE']
score = 1
password=(raw_input("please type in the password you would like rated:"))

if len(password) < 1:
      print strength[0]
if len(password) >=1 and len(password)<=4:
      print strength[1]
else:
    print ""

if len(password) >=7:
    score+=1
    print "password was made stronger by not being short"
else:
    print "Your password is really short, concider making it longer"

if len (password) >=10:
    score+=1
    print "password was made stronger by long"
else:
    print "An even longer password would make it stronger"

if re.search('[a-z]',password) and re.search('[A-Z]', password):
    score+=1
    print "password was made stronger by having upper & lower case letters"
else:
    print "Indlucing both upper and lower case letters will make your password stronger"

if re.search('[0-9]+', password):
    score+=1
    print "Password was made stronger by using numbers"
else:
    print "Using numbers will make your password stronger"

if re.search('[.,!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]',password):
    score+=1
    print "Password was made stronger by using punctuation marks and characters"
else:
    print "Using punctuation marks and characters will make the password stronger"

print "\n final password rating is:"
print strength[score]

我希望做的是:

第一个 - 为我提供给用户的密码内容的 cmets 添加颜色,好的 cmets 如:"password was made stronger by using numbers" 将有一个绿色输出,而建设性反馈如 "using numbers will make your password stronger" 将有一个红色输出,让用户更容易发现密码的优缺点

第二 - 我想知道,如果它的工作原理相同,我可以在上面的“强度”列表中为某些项目着色吗?让前两个红色,中间一对黄色,最后一对绿色?

ty!

【问题讨论】:

  • 嘿,伙计——我实际上已经回答了这个问题,但由于它看起来像是一张空白纸上的颜色设置,我真的不明白如何将它合并到已经制作的代码中的特定行中。
  • 拼写错误...如果您在意... sause = sauce, avage = 一般, concider = 考虑
  • 我很在意,谢谢!这只是我在几分钟内完成的一件快事——还没有进行拼写检查。

标签: python colors


【解决方案1】:

如果您的控制台(如您的标准 ubuntu 控制台)理解 ANSI color codes,您可以使用它们。

这里是一个例子:

print ('This is \x1b[31mred\x1b[0m.')

您也可以使用适用于空闲、终端和 PowerShell 的“clrprint”模块

pip install clrprint

from clrprint import *
clrhelp() # print's available colors and usage

user_input = clrinput("input please: ",clr='r') # just like input() [color is red]
clrprint('your text',user_input,clr='green') # just like print() 

【讨论】:

  • 我在 ubuntu 12.04 atm 中使用 IDLE ...如果它支持它,你介意根据我的代码给我一个小例子吗?即使是 1 行?我从那里弄清楚。
  • IDLE 不支持 ANSI 颜色代码。
  • @Giladiad 我添加了一个示例。
【解决方案2】:

IDLE 的控制台不支持 ANSI 转义序列,或任何其他形式的用于为输出着色的转义。

您可以学习如何直接与 IDLE 的控制台对话,而不是像普通标准输出一样对待它并打印到它(这就是它对语法进行颜色编码的方式),但这非常复杂。 idle 文档只是告诉您使用 IDLE 本身的基础知识,它的 idlelib 库没有文档(嗯,只有一行文档——“(2.3 中的新功能)支持库用于 IDLE 开发环境。”——如果您知道在哪里可以找到它,但这不是很有帮助)。因此,您需要read the source,或者进行大量的反复试验才能开始使用。


或者,您可以从命令行而不是 IDLE 运行脚本,在这种情况下,您可以使用终端处理的任何转义序列。大多数现代终端将至少处理基本的 16/8 色 ANSI。许多将处理 16/16,或扩展的 xterm-256 颜色序列,甚至是完整的 24 位颜色。 (我相信 gnome-terminal 是 Ubuntu 的默认配置,在其默认配置中它将处理 xterm-256,但这对于 SuperUser 或 AskUbuntu 来说确实是一个问题。)

学习阅读termcap 条目以了解要输入哪些代码是很复杂的……但如果您只关心单个控制台,或者愿意假设“几乎所有东西都能处理基本的 16/ 8 色 ANSI,以及任何不是的,我都不关心”,您可以忽略该部分,并根据例如 this page 对它们进行硬编码。

一旦您知道要发出什么,只需在打印之前将代码放入字符串中即可。

但是有一些库可以让您更轻松地完成这一切。一个非常好的库,它内置于 Python,是curses。这使您可以接管终端并执行全屏 GUI,包括颜色和旋转光标以及您想要的任何其他内容。当然,对于简单的用途来说,它有点重。其他库可以像往常一样通过搜索PyPI找到。

【讨论】:

    【解决方案3】:

    由于对 python 非常陌生而不知所措,我错过了这里给出的一些非常简单和有用的命令:Print in terminal with colors using Python? -

    最终决定使用 CLINT 作为由伟大而聪明的人给出的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2017-01-12
      相关资源
      最近更新 更多