­

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