Python风骚的打印

 

大家平时在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('\033[30m打印前景色0\033[0m')
 2print('\033[31m打印前景色1\033[0m')
 3print('\033[32m打印前景色2\033[0m')
 4print('\033[33m打印前景色3\033[0m')
 5print('\033[34m打印前景色4\033[0m')
 6print('\033[35m打印前景色5\033[0m')
 7print('\033[36m打印前景色6\033[0m')
 8print('\033[37m打印前景色7\033[0m')
 9print('\033[40m打印背景色0\033[0m')
10print('\033[41m打印背景色1\033[0m')
11print('\033[42m打印背景色2\033[0m')
12print('\033[43m打印背景色3\033[0m')
13print('\033[44m打印背景色4\033[0m')
14print('\033[45m打印背景色5\033[0m')
15print('\033[46m打印背景色6\033[0m')
16print('\033[47m打印背景色7\033[0m')
17print('\033[0m打印显示方式0\033[0m')
18print('\033[1m打印显示方式1\033[0m')
19print('\033[4m打印显示方式4\033[0m')
20print('\033[5m打印显示方式5\033[0m')
21print('\033[7m打印显示方式7\033[0m')
22print('\033[8m打印显示方式8\033[0m')
23print('\033[5;31;47m综合打印\033[0m')

颜色类型打印

每条默认的\033[0m为回复终端默认
最后一个\033[5;31;47m综合打印为使用闪烁方式红色字体白色背景色打印文字!

参数说明:

前景色背景色颜色
3040黑色
3141红色
3242绿色
3343黃色
3444洋红
3646青色
3747白色
显示方式意义
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('恢复默认')

自动恢复默认

希望今天的内容大家能喜欢….

作者:华为云特约供稿开发者  清风Python

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐