Python学习-终端字体高亮显示1
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
Python学习-终端字体高亮显示1的更多相关文章
- Python学习-终端字体高亮显示
1.采用原生转义字符序列,对Windows有的版本不支持(比如win7),完美支持Linux 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以 ...
- Python学习随笔:使用xlwings设置和操作excel多行多列数据以及设置数据字体颜色填充色对齐方式的方法
☞ ░ 前往老猿Python博文目录 ░ 在前面老猿的文章中,<Python学习随笔:使用xlwings读取和操作Excel文件>.<Python学习随笔:使用xlwings读取和操 ...
- Python之路【第二十四篇】:Python学习路径及练手项目合集
Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Py ...
- 色彩缤纷的python(改变字体颜色及样式不完全版)
色彩缤纷的python(改变字体颜色及样式) *补上昨天随笔中提到的改变字体颜色样式的方法,昨日随笔https://www.cnblogs.com/Du704/p/11265958.html 在项目过 ...
- 色彩缤纷的Python(改变字体颜色及样式)
色彩缤纷的python(改变字体颜色及样式) 在项目过程中,我们常常会因为输出信息的颜色与样式过于单调以至于让人在视觉上感到很杂乱,所以看下文: 在Linux终端中,使用转义序列来进行如上所述的显示, ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- python学习心得第一章
初始python 1什么是程序 计算机程序是一组执行某种动作的的指令.和那些电路.芯片.显卡.硬盘等不同,它不是计算机本身可以触摸的部分,而是隐藏在背后运行在硬件上面的东西.程序就是一系列告诉没有知觉 ...
- Python学习资料整理以及书籍、开发工具推荐
我不知道大家学习Python的时候是不是和我一样感觉很无助,不知道在入门或者进阶的时候应该掌握哪些知识点,下面我就梳理下我自己学习Python开 发的过程及资料分享给大家,这些方法资料可能并不适合所有 ...
- python 学习笔记 9 -- Python强大的自省简析
1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:“见贤思齐焉,见不贤而内自省也.”(<论语·里仁>)当然,我们今天不 ...
随机推荐
- C/C++ 运算符优先级(转载)
最讨厌这个了.在这里记录下. 优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的 ...
- 逆袭之旅DAY24.XIA.二重进阶、双色球
一. 选择题. 1. 以下关于二重循环的说法正确的是(D). A. 二重循环就是一般程序中只能有两个循环 B. While循环不能嵌套在for循环里 C. 两个重叠的循环不能嵌套在第三个循环里. D. ...
- xadmin后台导出时gunicorn报错ascii
django + xadmin + nginx + gunicorn部署后,xadmin后台导出model数据报错,gunicorn日志记录为:UnicodeEncodeError: 'ascii' ...
- Python3 线程/进程池 concurrent.futures
python3之concurrent.futures一个多线程多进程的直接对接模块,python3.2有线程池了 Python标准库为我们提供了threading和multiprocessing模块编 ...
- java 实现简单链式队列
package com.my; /** * 链式队列 * @author wanjn * */ public class LinkedQueue { private Node head; privat ...
- 以黄门镇黄湾村某一扶贫文档为例——将Excel数据填入到已存在的Word模板
傻瓜可以写出机器读得懂代码,但写出让人能读懂的代码的是优秀程序员 作用:通过Excel文件中的一列数据作为文件名创建Word文档,并将Excel中的一行数据填一表,实现自动化 Excel的VBA宏代码 ...
- linux系统安装jdk详细配置
1.通过指令 whereis java 查看是否已经配置jdk 如果已经安装,通过指令 rm -rf <jdk路径> 删除 2.通过ssh工具将jdk-8u11-linux-x64.tar ...
- DevExpress WinForms使用教程:WinForms Sunburst控件
[DevExpress WinForms v18.2下载] DevExpress WinForms v18.2中包含了一个新的WinForms组件 - WinForms Sunburst,它旨在帮助开 ...
- powerdesigner 实体关系模型CDM与物理数据模型PDM互转
1.创建CDM 2.CDM转换PDM 3.PDM转CDM 环境 powerdesigner15.1 1.创建CDM File --> new Model-->Conceptual data ...
- linux的python版本升级
可利用Linux自带下载工具wget下载,如下所示: # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.13.tgz 下载完成后 ...