在開發程序中經常碰到在本地環境無法完成聯調測驗的情況,必須到統一的聯機環境對接其他系統測驗,往往是出現了BUG難以查找資料記錄及時定位到錯誤出現的位置,
【閱讀全文】
面對這種情況可能情況可能是一個簡單的BUG導致的,但是定位問題往往就需要很長的時間,在python編程中推薦非標準庫tabulate,它可以將程式運行程序中產生的資料記錄格式化的列印出來很方便我們定位問題,
通過結合簡單的日志非標準庫loguru可以很快的列印出又美觀又實用的日志記錄,loguru非標準庫其實在一些文章的原始碼示例中我們已經在使用了,
安裝程序還是通過pip的方式直接安裝,由于loguru、tabulate都是python的非標準庫,因此,沒有安裝的話需要安裝一下,默認我們都使用的清華大學的python鏡像站,大家可以選擇其他的鏡像站都可以,
pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install tabulate - i https://pypi.tuna.tsinghua.edu.cn/simple/
做好準備作業以后將loguru、tabulate模塊都匯入進來就OK了,沒有其他復雜的操作!
# It's a shortcut to create a logger with the default configuration.
from loguru import logger
# It's importing the function `tabulate` from the module `tabulate`.
from tabulate import tabulate
關于非標準庫tabulate,它的列印模式其實有很多,我們平常使用到的可能就是幾種比較常見的,下面將tabulate所有的列印模式全部列舉出來,有需要的大佬可以參考,
'''
"plain"
"simple"
"github"
"grid"
"fancy_grid"
"pipe"
"orgtbl"
"jira"
"presto"
"psql"
"rst"
"mediawiki"
"moinmoin"
"youtrack"
"html"
"latex"
"latex_raw"
"latex_booktabs"
"textile"
'''
我們選擇其中的一種'grid'模式來看看效果如何,因為這種模式列印的資料記錄也是比較常見的,下面創建一個函式tab_grid_log列印一段陣列形式的資料記錄,
def tab_grid_log():
"""
> This function takes a list of lists and returns a list of lists with the log of each element
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `grid` format.
logger.info(tabulate(table, headers=header, tablefmt='grid'))
tab_grid_log()
# 2022-09-17 18:33:00.472 | INFO | __main__:tab_grid_log:66 - +--------+--------+--------+--------+--------+
# | 姓名 | 年齡 | 班級 | 成績 | 表現 |
# +========+========+========+========+========+
# | Python | 20 | 1710 | 98 | 5 |
# +--------+--------+--------+--------+--------+
# | Java | 22 | 1810 | 98 | 4.9 |
# +--------+--------+--------+--------+--------+
#
# Process finished with exit code 0
使用grid模式的列印的資料記錄就是宮格形式很便于查找日志中的資料記錄,也是我們經常在日志記錄中使用的一種列印方法,
接下來我們隨便選擇一種模式再次進行列印,這里就選擇html模式來看看效果吧,這種模式之前沒有使用過我很好奇它會列印出什么樣的效果,
def tab_html_log():
"""
> This function takes a log file and returns a html table of the log file
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `html` format.
logger.info(tabulate(table, headers=header, tablefmt='html'))
tab_html_log()
# 2022-09-17 18:37:50.383 | INFO | __main__:tab_html_log:87 - <table>
# <thead>
# <tr><th>姓名 </th><th style="text-align: right;"> 年齡</th><th style="text-align: right;"> 班級</th><th style="text-align: right;"> 成績</th><th style="text-align: right;"> 表現</th></tr>
# </thead>
# <tbody>
# <tr><td>Python</td><td style="text-align: right;"> 20</td><td style="text-align: right;"> 1710</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 5 </td></tr>
# <tr><td>Java </td><td style="text-align: right;"> 22</td><td style="text-align: right;"> 1810</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 4.9</td></tr>
# </tbody>
# </table>
從列印結果可以看出來了,使用html模式的列印時實際上是生成html的原始碼,這還是很智能的包括style的樣式屬性也填充了,
【精彩推薦】
問題記錄:為什們有的python底層代碼塊函式卻只有一個pass?
我使用pangu模塊做了一個文本格式化小工具!
ping命令的多種玩法,以前竟然只用它來測驗網速!
歡迎關注作者公眾號【Python 集中營】,專注于后端編程,每天更新技術干貨,不定時分享各類資料!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511910.html
標籤:Python
上一篇:python-繪圖與可視化
