Python風騷的顏色輸出與進度條列印
- 2019 年 11 月 27 日
- 筆記
大家平時在Linux/Windows下安裝軟體時,經常會出現進度條和百分比的提示,Python是否能實現這樣的列印?安裝過程中,經常會看到很多帶顏色的安裝說明,我們在python輸出時,確是千篇一律的黑底白色,是否想過列印的炫酷一些呢?
操作其實很簡單,今天就來教教大家,通過幾分鐘的學習讓之後程式碼的輸出變得與眾不同!
Python列印進度條
python列印進度條的原理其實很簡單,先讓我們看一個例子吧:
1# -*- coding: utf-8 -*- 2# @Author : 王翔 3# @WeChat : King_Uranus 4# @公眾號 : 清風Python 5# @Date : 2019/9/16 22:09 6# @Software : PyCharm 7# @version :Python 3.7.3 8# @File : ProgressBar.py 9 10import time 11 12def progress_bar(total): 13 if total <= 0: 14 raise ValueError("Wrong total number ...") 15 # step = (100 // total if total <= 100 else total // 100) 16 17 for i in range(0, total): 18 time.sleep(0.05) 19 step = int(100 / total * (i + 1)) 20 str1 = 'r[%3d%%] %s' % (step, '>' * step) 21 print(str1, end='', flush=True) 22 23progress_bar(20) 24print() 25progress_bar(110)
列印進度條
我們通過自己實現了進度條的展示,那麼python是否具備現成的模組呢?答案是Yes! tqdm
Tqdm 是一個快速,可擴展的Python進度條,可以在 Python 長循環中添加一個進度提示資訊,用戶只需要封裝任意的迭代器 tqdm(iterator)。 安裝:
pip install tqdm
來看一個例子:
1from tqdm import tqdm 2import string 3import time 4 5for char in tqdm(string.ascii_uppercase): 6 time.sleep(0.1) 7 8for i in tqdm(range(50)): 9 time.sleep(0.05)

tqdm進度條
tqdm的強大遠不止此,喜歡的朋友可以去它的git網址詳細學習:https://github.com/tqdm/tqdm
Python帶色彩輸出
python顏色輸出其實只是調用了命令號的相關特殊標記,shell中我們也經常使用它:
1print('33[30m列印前景色033[0m') 2print('33[31m列印前景色133[0m') 3print('33[32m列印前景色233[0m') 4print('33[33m列印前景色333[0m') 5print('33[34m列印前景色433[0m') 6print('33[35m列印前景色533[0m') 7print('33[36m列印前景色633[0m') 8print('33[37m列印前景色733[0m') 9print('33[40m列印背景色033[0m') 10print('33[41m列印背景色133[0m') 11print('33[42m列印背景色233[0m') 12print('33[43m列印背景色333[0m') 13print('33[44m列印背景色433[0m') 14print('33[45m列印背景色533[0m') 15print('33[46m列印背景色633[0m') 16print('33[47m列印背景色733[0m') 17print('33[0m列印顯示方式033[0m') 18print('33[1m列印顯示方式133[0m') 19print('33[4m列印顯示方式433[0m') 20print('33[5m列印顯示方式533[0m') 21print('33[7m列印顯示方式733[0m') 22print('33[8m列印顯示方式833[0m') 23print('33[5;31;47m綜合列印33[0m')
顏色類型列印
每條默認的33[0m為回復終端默認 最後一個33[5;31;47m綜合列印為使用閃爍方式紅色字體白色背景色列印文字!
參數說明:
前景色 | 背景色 | 顏色 |
|---|---|---|
30 | 40 | 黑色 |
31 | 41 | 紅色 |
32 | 42 | 綠色 |
33 | 43 | 黃色 |
34 | 44 | 洋紅 |
36 | 46 | 青色 |
37 | 47 | 白色 |
顯示方式 | 意義 |
|---|---|
0 | 終端默認設置 |
1 | 高亮顯示 |
22 | 非高亮顯示 |
4 | 使用下劃線 |
24 | 去下劃線 |
5 | 閃爍 |
25 | 去閃爍 |
7 | 反白顯示 |
27 | 非反顯 |
8 | 不可見 |
28 | 可見 |
那麼和上面一樣的套路,python中是否有模組能實現這種顏色列印的功能呢?答案依然是Yes! colorama
Python的Colorama模組,可以跨多終端,顯示字體不同的顏色和背景,只需要導入colorama模組即可,不用再每次都像linux一樣指定顏色。 pip install colorama Fore是針對字體顏色,Back是針對字體背景顏色,Style是針對字體格式 Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Style: DIM, NORMAL, BRIGHT, RESET_ALL
1>>> from colorama import Fore, Back, Style 2>>> print(Fore.RED + '列印紅色文字') 3>>> 列印紅色文字 4>>> print(Back.GREEN + '設置背景為綠色') 5>>> 設置背景為綠色 6>>> print(Style.RESET_ALL) 7>>> print('恢復默認') 8>>> 恢復默認
列印顏色示例
細心的網友看到,我們如果沒有恢復默認的話,會繼承上面的顏色狀態。那麼,如何像剛才一樣,每次輸出後自動化恢復呢?
1from colorama import init, Fore, Back, Style 2 3init(autoreset=True) 4print(Fore.RED + '列印紅色文字') 5print(Back.GREEN + '設置背景為綠色') 6print('恢復默認')
自動恢復默認
關於裝13,只能幫大家到這裡了,希望今天的內容大家能喜歡….
END

