日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python学习-终端字体高亮显示

發(fā)布時(shí)間:2023/12/18 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python学习-终端字体高亮显示 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、采用原生轉(zhuǎn)義字符序列,對(duì)Windows有的版本不支持(比如win7),完美支持Linux

實(shí)現(xiàn)過程:

終端的字符顏色是用轉(zhuǎn)義序列控制的,是文本模式下的系統(tǒng)顯示功能,和具體的語言無關(guān)。

轉(zhuǎn)義序列是以ESC開頭,即用\033來完成(ESC的ASCII碼用十進(jìn)制表示是27,用八進(jìn)制表示就是033)。

格式:

? ? ?開頭部分:\033[顯示方式;前景色;背景色m +?結(jié)尾部分:\033[0m

? ? ?注意:開頭部分的三個(gè)參數(shù):顯示方式,前景色,背景色是可選參數(shù),可以只寫其中的某一個(gè);另外由于表示三個(gè)參數(shù)不同含義的數(shù)值都是唯一的沒有重復(fù)的,所以三個(gè)參數(shù)的書寫先后順序沒有固定要求,系統(tǒng)都能識(shí)別;但是,建議按照默認(rèn)的格式規(guī)范書寫。 ? ? ?對(duì)于結(jié)尾部分,其實(shí)也可以省略,但是為了書寫規(guī)范,建議\033[***開頭,\033[0m結(jié)尾。 用這種原生的轉(zhuǎn)義序列輸出,在linux下完全支持,但是在windows下確存在兼容問題,比如在win10下可以正常顯示顏色,在win7下確不支持。因此可以使用python標(biāo)準(zhǔn)庫提供的colorama模塊輸出彩色字體,這個(gè)模塊是跨平臺(tái)的,內(nèi)部實(shí)現(xiàn)也是采用轉(zhuǎn)義序列來顯示顏色的,只不過對(duì)windows平臺(tái)做了特殊處理,因此完全兼容linux和windows各個(gè)版本。 數(shù)值參數(shù)表示的意義: 說明:
前景色背景色顏色
3040黑色
3141紅色
3242綠色
3343黃色
3444藍(lán)色
3545紫紅色
3646青藍(lán)色
3747白色

顯示方式:

顯示方式意義
0終端默認(rèn)設(shè)置
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 終端默認(rèn)設(shè)置 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 藍(lán)色 27 # 35 45 紫紅色 28 # 36 46 青藍(lán)色 29 # 37 47 白色 30 #--------------------------------------- 31 class Colored(object): 32 RED = '\033[31m' #紅色 33 GREEN = '\033[32m' #綠色 34 YELLOW = '\033[33m' #黃色 35 BLUE = '\033[34m' #藍(lán)色 36 FUCHSIA = '\033[35m' #紫紅色 37 CYAN = '\033[36m' #青藍(lán)色 38 WHITE = '\033[37m' #白色 39 #:no color 40 RESET = '\033[0m' #終端默認(rèn)顏色 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!'))

?輸出結(jié)果:

2、采用Python標(biāo)準(zhǔn)庫colorama模塊--兼容linux和windows各個(gè)版本:

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是一個(gè)python專門用來在控制臺(tái)、命令行輸出彩色文字的模塊,可以跨平臺(tái)使用 9 # 在windows下linux下都工作良好,如果你想讓控制臺(tái)的輸出信息更漂亮一些,可以使用給這個(gè)模塊。 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!')) 輸出結(jié)果: ?3、采用Python的termcolor模塊:

termcolor是一個(gè)python包,可以改變控制臺(tái)輸出的顏色,支持各種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)

?

輸出結(jié)果:

?

參考:

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

轉(zhuǎn)載于:https://www.cnblogs.com/yangshijia/p/8969271.html

總結(jié)

以上是生活随笔為你收集整理的Python学习-终端字体高亮显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。