python 美化输出信息的实例
作者:晓东邪 时间:2022-04-15 09:53:54
如下所示:
# -*- coding: utf-8 -*-
# @Author: xiaodong
# @Date: just hide
# @Last Modified by: xiaodong
# @Last Modified time: just hide
# try:
# from colorama import Fore, Style
# except ImportError:
# class Temp:
# def __getattr__(self, x):
# return ''
# Fore = Style = Temp()
STYLE = {
'fore': {
'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
'blue': 34, 'purple': 35, 'cyan': 36, 'white': 37,
},
'back': {
'black': 40, 'red': 41, 'green': 42, 'yellow': 43,
'blue': 44, 'purple': 45, 'cyan': 46, 'white': 47,
},
'mode': {
'bold': 1, 'underline': 4, 'blink': 5, 'invert': 7,
},
'default': {
'end': 0,
}
}
def use_style(string, mode='', fore='', back=''):
mode = '%s' % STYLE['mode'][mode] if mode in STYLE['mode'] else ''
fore = '%s' % STYLE['fore'][fore] if fore in STYLE['fore'] else ''
back = '%s' % STYLE['back'][back] if back in STYLE['back'] else ''
style = ';'.join([s for s in [mode, fore, back] if s])
style = '\033[%sm' % style if style else ''
end = '\033[%sm' % STYLE['default']['end'] if style else ''
return '%s%s%s' % (style, string, end)
def gentle_show(seq, *, column=4, fontdict=None):
if fontdict is None:
line_color = 'red'
font_color = 'blue'
elif isinstance(fontdict, dict):
line_color = fontdict.get('line_color', 'red')
font_color = fontdict.get('font_color', 'green')
seq = list(map(str, seq))
max_len = len(max(seq, key=len))
for index, ele in enumerate(seq):
if index % column == 0:
print(use_style('-' * max_len * column + '-' * (column - 1), fore=line_color))
print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color), end='|')
else:
if (index - column + 1) % column == 0:
print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color))
else:
print(use_style(ele.center(max_len, ' '), mode='bold', fore=font_color), end='|')
print('\n')
if __name__ == "__main__":
gentle_show(dir([]), column=6, fontdict={'line_color': 'red', 'font_color': 'green'})
gentle_show(range(10))
来源:https://blog.csdn.net/xiaodongxiexie/article/details/79918158
标签:python,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Selenium 之关闭窗口close与quit的方法
2023-11-14 01:28:06
python关键字传递参数实例分析
2023-08-24 04:28:34
accept-charset与Header P3P
2009-04-01 18:43:00
Django使用unittest模块进行单元测试过程解析
2021-04-03 13:09:08
典型的三行二列居中高度自适应css布局
2008-02-22 16:02:00
![](https://img.aspxhome.com/file/UploadPic/20082/22/2008222164924132s.gif)
Oracle 语句优化分析说明第1/2页
2009-09-18 13:23:00
浅谈python中真正关闭socket的方法
2023-11-02 15:41:56
asp中判断是否是手机浏览器以及手机类型
2014-12-06 09:33:05
一文搞懂Python中is和==的区别
2023-11-15 09:42:27
js和asp操作fso比较
2007-09-23 09:17:00
Python中的getopt函数使用详解
2023-04-07 03:08:00
防止表格或或div层被撑开的几种方法
2008-01-01 15:33:00
python文字和unicode/ascll相互转换函数及简单加密解密实现代码
2023-08-23 08:13:59
Mootools常用方法扩展(二)
2009-01-11 18:22:00
Web Forms 2.0
2008-07-24 12:47:00
Django应用程序中如何发送电子邮件详解
2023-11-04 02:07:54
![](https://img.aspxhome.com/file/2023/2/65452_0s.png)
Python&Matlab实现樱花的绘制
2023-11-04 05:07:16
![](https://img.aspxhome.com/file/2023/2/63902_0s.gif)
Python 并行化执行详细解析
2021-09-23 22:20:52
![](https://img.aspxhome.com/file/2023/0/70680_0s.png)
Python爬虫简单运用爬取代理IP的实现
2021-08-25 23:26:42
![](https://img.aspxhome.com/file/2023/1/75201_0s.jpg)
Bootstrap select多选下拉框实现代码
2023-09-15 12:44:55
![](https://img.aspxhome.com/file/2023/6/56316_0s.jpg)