Python学习-终端字体高亮显示
1、采用原生转义字符序列,对Windows有的版本不支持(比如win7),完美支持Linux
实现过程:
终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关。
转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033)。
格式:
开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m
前景色 | 背景色 | 颜色 |
30 | 40 | 黑色 |
31 | 41 | 红色 |
32 | 42 | 绿色 |
33 | 43 | 黄色 |
34 | 44 | 蓝色 |
35 | 45 | 紫红色 |
36 | 46 | 青蓝色 |
37 | 47 | 白色 |
显示方式:
显示方式 | 意义 |
0 | 终端默认设置 |
1 | 高亮显示 |
4 | 使用下划线 |
5 | 闪烁 |
7 | 反白显示 |
8 | 不可见 |
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/4/29 10:27 4 # @Author : yang 5 # @File : Colored_Escape_character.py 6 # @Software: PyCharm 7 #-------------------------------- 8 #显示格式:\033[显示方式;前景色;背景色m 9 #-------------------------------- 10 #显示方式 说明 11 # 0 终端默认设置 12 # 1 高亮显示 13 # 4 使用下划线 14 # 5 闪烁 15 # 7 反白显示 16 # 8 不可见 17 # 22 非粗体 18 # 24 非下划线 19 # 25 非闪烁 20 # 21 #前景色 背景色 颜色 22 # 30 40 黑色 23 # 31 41 红色 24 # 32 42 绿色 25 # 33 43 黄色 26 # 34 44 蓝色 27 # 35 45 紫红色 28 # 36 46 青蓝色 29 # 37 47 白色 30 #--------------------------------------- 31 class Colored(object): 32 RED = '\033[31m' #红色 33 GREEN = '\033[32m' #绿色 34 YELLOW = '\033[33m' #黄色 35 BLUE = '\033[34m' #蓝色 36 FUCHSIA = '\033[35m' #紫红色 37 CYAN = '\033[36m' #青蓝色 38 WHITE = '\033[37m' #白色 39 #:no color 40 RESET = '\033[0m' #终端默认颜色 41 def color_str(self,color,s): 42 return '{}{}{}'.format(getattr(self,color),s,self.RESET) 43 44 def red(self,s): 45 return self.color_str('RED',s) 46 def green(self,s): 47 return self.color_str('GREEN',s) 48 def yellow(self,s): 49 return self.color_str('YELLOW',s) 50 def blue(self,s): 51 return self.color_str('BLUE',s) 52 def fuchsia(self,s): 53 return self.color_str('FUCHSIA',s) 54 def cyan(self,s): 55 return self.color_str('CYAN',s) 56 def white(self,s): 57 return self.color_str('WHITE',s) 58 #-----------使用示例如下-------- 59 color = Colored() 60 print(color.red('I am red!')) 61 print(color.green('I am green!')) 62 print(color.yellow('I am yellow!')) 63 print(color.blue('I am blue!')) 64 print(color.fuchsia('I am fuchsia!')) 65 print(color.cyan('I am cyan!')) 66 print(color.white('I am white!'))
输出结果:
2、采用Python标准库colorama模块--兼容linux和windows各个版本:
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/4/29 10:57 4 # @Author : yang 5 # @File : Colored_Colorama_module.py 6 # @Software: PyCharm 7 #--------------colorama模块的一些常量------- 8 #colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用 9 # 在windows下linux下都工作良好,如果你想让控制台的输出信息更漂亮一些,可以使用给这个模块。 10 # Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. 11 # Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. 12 # Style: DIM, NORMAL, BRIGHT, RESET_ALL 13 from colorama import init,Fore,Back,Style 14 #init(autoreset=True) 15 class Colored(object): 16 def red(self,s): 17 return Fore.RED + s + Fore.RESET 18 def green(self,s): 19 return Fore.GREEN + s + Fore.RESET 20 def yellow(self,s): 21 return Fore.YELLOW + s + Fore.RESET 22 def blue(self,s): 23 return Fore.BLUE + s + Fore.RESET 24 def magenta(self,s): 25 return Fore.MAGENTA + s + Fore.RESET 26 def cyan(self,s): 27 return Fore.CYAN + s + Fore.RESET 28 def white(self,s): 29 return Fore.WHITE + s + Fore.RESET 30 def balck(self,s): 31 return Fore.BLACK 32 def white_green(self,s): 33 return Fore.WHITE + Back.GREEN + s + Fore.RESET + Back.RESET 34 color = Colored() 35 print(color.red('I am red!')) 36 print(color.green('I am green!')) 37 print(color.yellow('I am yellow!')) 38 print(color.blue('I am blue!')) 39 print(color.magenta('I am magenta!')) 40 print(color.cyan('I am cyan!')) 41 print(color.white('I am white!')) 42 print(color.white_green('I am white green!'))
输出结果:
termcolor是一个python包,可以改变控制台输出的颜色,支持各种terminal(WINDOWS的cmd.exe除外)。
支持下列的文字颜色:
grey, red, green, yellow, blue, magenta, cyan, white
支持下列的背景高亮:
on_grey, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
支持下列属性:
bold, dark, underline, blink, reverse, concealed
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/4/29 16:49 4 # @Author : yang 5 # @File : Colored_Termcolor_module.py 6 # @Software: PyCharm 7 import sys 8 from termcolor import colored,cprint 9 text = colored('Hello,World!','red',attrs=['reverse','blink']) 10 11 #colored(text, color=None, on_color=None, attrs=None) 12 # Available text colors: 13 # red, green, yellow, blue, magenta, cyan, white. 14 15 # Available text highlights: 16 # on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white. 17 18 # Available attributes: 19 # bold, dark, underline, blink, reverse, concealed. 20 #print('\033[5;7;31mHello,World!\033[0m') 21 22 print(text) 23 24 cprint('Hello,World!','green','on_red') 25 #cprint('Hello,World!','green','on_red',attrs=['bold']) 26 #def cprint(text, color=None, on_color=None, attrs=None, **kwargs) 27 28 print_red_on_cyan = lambda x:cprint(x,'red','on_cyan') 29 print_red_on_cyan('Hello,World!') 30 print_red_on_cyan('Hello,Universe!') 31 for i in range(10): 32 cprint(i,'magenta',end=' ') 33 cprint('Attention!','red',attrs=['bold'],file = sys.stderr)
输出结果:
参考:
1、https://pypi.org/project/colorama/
2、https://pypi.org/project/termcolor/#description
3、https://www.cnblogs.com/hellojesson/p/5961570.html
4、https://stackoverflow.com/questions/287871/print-in-terminal-with-colors/3332860#3332860
转载于:https://www.cnblogs.com/yangshijia/p/8969271.html
Python学习-终端字体高亮显示相关推荐
- python 字体颜色_超萌新级的Python学习心得——字体颜色
概要:本文讲的是Python语言中在命令行中显示不同字体颜色的方法,如有错误,欢迎指正. 正文: 开发过程中,为了方便调试代码,以及处理错误信息,基本上是需要输出一些文本的,例如在使用try-catc ...
- Python:终端打印字体颜色
简介:日常开发中,经常需要打印显示各种信息.海量的信息堆砌在控制台中,就会导致各种信息都显示在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上差异的字体颜色,那么就会更加显眼,增加使用者可 ...
- Python 修改终端输入字体颜色值
#!/bin/sh # Python 在终端中修改字体的颜色值 需要在字体中输入开头部分添加\033[x;x;xm 结尾部分添加\033[0m # x 对应下面的内容 ""&quo ...
- python学习 - 图标签用宋体Times New Roman字体 + 规范的混淆矩阵绘制
python学习 - 图标签用宋体&Times New Roman字体 + 规范的混淆矩阵绘制 只需复制下面一行代码即可获得效果 中文:宋体字号 英文和数字:Times New Roman字体 ...
- Ubuntu下,Python版本查询、默认版本更改、终端字体背景颜色设置、Ubuntu中文语言设置、VMware中界面大小调整
Ubuntu下,Python初步配置及终端设置 1.Python 版本查询 2.如何修改Ubuntu默认Python版本 3.Ubuntu终端字体颜色调整 4.Ubuntu中文语言设置 5.VMwar ...
- 色彩缤纷的python(改变字体颜色及样式)
在项目过程中,我们常常会因为输出信息的颜色与样式过于单调以至于让人在视觉上感到很杂乱,所以看下文: 在Linux终端中,使用转义序列来进行如上所述的显示,转义序列以ESC开头,即ASCII码下的\03 ...
- 开始Python学习第二课(python环境安装)
*** 开始Python学习第二课*** 一. Python语言简介 1. Python语言 1.1 Python的命名 2. Python基本概念 2.1 Python的简单介绍 2.2 Pytho ...
- python学习笔记连载
python学习连载 一.python的下载.安装及配置 1.python下载 python下载地址widows版:https://www.python.org/downloads/windows/ ...
- 小猪的Python学习之旅 —— 16.采集拉勾网数据分析Android就业行情
小猪的Python学习之旅 -- 16.再尝Python数据分析:采集拉勾网数据分析Android就业行情 标签:Python 一句话概括本文: 爬取拉钩Android职位相关数据,利用numpy,p ...
最新文章
- 微信支付的架构真的那么牛吗?
- 常见的几个js笔试题
- ORACLE网络连接配置与文件:listener.ora、sqlnet.ora、tnsnames.ora
- java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core
- python怎么帮助运营 进行数据管理_注意!这里手把手教你做数据运营管理
- css input光标粗细,如何用CSS原生属性caret-color改变input输入框光标颜色
- Spring循环依赖的三种方式,你都清楚吗?
- 【转】python模块分析之collections(六)
- laravel 运用
- php无法调用shell脚本文件,php调用shell脚本--126错误
- 基于Python的豆瓣中文影评差评分析
- Codeforces Round #444 (Div. 2)-贪心尺取-Ratings and Reality Shows
- 如何开发微信礼品卡-服务端
- 链上天眼2.0版本上线,欧科云链深耕区块链大数据产业成绩斐然
- Java—将数据生成pdf表格
- 性能测试面试题(测试框架总结)史上最全面试题
- Linux学习:文件和用户管理——文件管理命令
- MyEclipse小结
- windows下frp socket内网穿透体验
- OpenStack Horizon 参数设置说明
热门文章
- 【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)
- 河南省高考让不让带计算机,河南高考2018严禁携带的东西有哪些?这种衣服不能进考场...
- android 单机斗地主,单机斗地主
- 【算法系列之十】三数之和
- c#解决在数据表格中无法显示秒数问题
- gradient设置上下渐变_PaintCode Mac使用教程:如何使用渐变色
- leetcode203 移除链表元素
- python遍历txt每一行_python – 计算(和写入)文本文件中每一行的...
- STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa
- Redis Mac下安装与使用