是否可以为python输出添加颜色? [英] is it possible to add colors to python output?

查看:134
本文介绍了是否可以为python输出添加颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

so i made a small password strength tester for me, my friends and my family, as seen here:

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]

我希望做的是:

1st-在我给用户的有关内容的注释中添加颜色他们的密码,良好的注释,例如:通过使用数字使密码更强 将显示为绿色,同时提供建设性的反馈,例如使用数字将使您的密码更强。将显示为红色,使用户更容易发现其密码的优缺点

1st - add color to the comments i've given the user about the content of their password, good comments such as the: "password was made stronger by using numbers" will have a green output, while constructive feedback such as the "using numbers will make your password stronger" will have a red output, making it easier for the user to spot the pros and cons of his password

2nd-我想知道,如果它能正常工作,我可以为上面的强度列表中的某些项目上色吗?使前两个红色,中间对黄色和最后一对绿色?

2nd - i was wondering, if it works the same, can i color certain items in my above "strength" list? making the first two red, the middle pair yellow and the last pair green?

ty!

推荐答案

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

IDLE's console does not support ANSI escape sequences, or any other form of escapes for coloring your output.

您可以学习如何与直接使用IDLE的控制台,而不是像对待普通stdout一样对待它并进行打印(这是对语法进行颜色编码的方式),但这非常复杂。 idle 文档只是告诉您使用IDLE本身的基础知识,其 idlelib 库没有任何文档(嗯, 是一行文档-( 2.3)支持IDLE开发环境的库。-如果您知道在何处找到它,但这不是很有帮助)。因此,您需要阅读源文件,或者做很多事情反复尝试,甚至开始使用。

You can learn how to talk to IDLE's console directly instead of just treating it like normal stdout and printing to it (which is how it does things like color-coding your syntax), but that's pretty complicated. The idle documentation just tells you the basics of using IDLE itself, and its idlelib library has no documentation (well, there is a single line of documentation—"(New in 2.3) Support library for the IDLE development environment."—if you know where to find it, but that isn't very helpful). So, you need to either read the source, or do a whole lot of trial and error, to even get started.

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

Alternatively, you can run your script from the command line instead of from IDLE, in which case you can use whatever escape sequences your terminal handles. Most modern terminals will handle at least basic 16/8-color ANSI. Many will handle 16/16, or the expanded xterm-256 color sequences, or even full 24-bit colors. (I believe gnome-terminal is the default for Ubuntu, and in its default configuration it will handle xterm-256, but that's really a question for SuperUser or AskUbuntu.)

学习阅读 termcap 条目以了解要输入的 代码很复杂……但是如果您只关心一个控制台-或只想假设几乎所有东西都可以处理基本的16/8色ANSI,而所有不关心的东西,我都不关心,则可以忽略该部分并对其进行硬编码基于例如此页面

Learning to read the termcap entries to know which codes to enter is complicated… but if you only care about a single console—or are willing to just assume "almost everything handles basic 16/8-color ANSI, and anything that doesn't, I don't care about", you can ignore that part and just hardcode them based on, e.g., this page.

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

Once you know what you want to emit, it's just a matter of putting the codes in the strings before printing them.

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

But there are libraries that can make this all easier for you. One really nice library, which comes built in with Python, is curses. This lets you take over the terminal and do a full-screen GUI, with colors and spinning cursors and anything else you want. It is a little heavy-weight for simple uses, of course. Other libraries can be found by searching PyPI, as usual.

这篇关于是否可以为python输出添加颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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