Python tabulate結(jié)合loguru打印出美觀方便的日志記錄
在開發(fā)過程中經(jīng)常碰到在本地環(huán)境無法完成聯(lián)調(diào)測(cè)試的情況,必須到統(tǒng)一的聯(lián)機(jī)環(huán)境對(duì)接其他系統(tǒng)測(cè)試。往往是出現(xiàn)了BUG難以查找數(shù)據(jù)記錄及時(shí)定位到錯(cuò)誤出現(xiàn)的位置。
面對(duì)這種情況可能情況可能是一個(gè)簡(jiǎn)單的BUG導(dǎo)致的,但是定位問題往往就需要很長(zhǎng)的時(shí)間。在python編程中推薦非標(biāo)準(zhǔn)庫tabulate,它可以將程序運(yùn)行過程中產(chǎn)生的數(shù)據(jù)記錄格式化的打印出來很方便我們定位問題。
通過結(jié)合簡(jiǎn)單的日志非標(biāo)準(zhǔn)庫loguru可以很快的打印出又美觀又實(shí)用的日志記錄,loguru非標(biāo)準(zhǔn)庫其實(shí)在一些文章的源碼示例中我們已經(jīng)在使用了。
安裝過程還是通過pip的方式直接安裝,由于loguru、tabulate都是python的非標(biāo)準(zhǔn)庫,因此,沒有安裝的話需要安裝一下。默認(rèn)我們都使用的清華大學(xué)的python鏡像站,大家可以選擇其他的鏡像站都可以。
pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install tabulate - i https://pypi.tuna.tsinghua.edu.cn/simple/
做好準(zhǔn)備工作以后將loguru、tabulate模塊都導(dǎo)入進(jìn)來就OK了,沒有其他復(fù)雜的操作!
# 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
關(guān)于非標(biāo)準(zhǔn)庫tabulate,它的打印模式其實(shí)有很多,我們平常使用到的可能就是幾種比較常見的,下面將tabulate所有的打印模式全部列舉出來,有需要的大佬可以參考。
''' "plain" "simple" "github" "grid" "fancy_grid" "pipe" "orgtbl" "jira" "presto" "psql" "rst" "mediawiki" "moinmoin" "youtrack" "html" "latex" "latex_raw" "latex_booktabs" "textile" '''
我們選擇其中的一種'grid'模式來看看效果如何,因?yàn)檫@種模式打印的數(shù)據(jù)記錄也是比較常見的。下面創(chuàng)建一個(gè)函數(shù)tab_grid_log打印一段數(shù)組形式的數(shù)據(jù)記錄。
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'班級(jí)', u'成績(jī)', u'表現(xiàn)'] # 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 - +--------+--------+--------+--------+--------+ # | 姓名 | 年齡 | 班級(jí) | 成績(jī) | 表現(xiàn) | # +========+========+========+========+========+ # | Python | 20 | 1710 | 98 | 5 | # +--------+--------+--------+--------+--------+ # | Java | 22 | 1810 | 98 | 4.9 | # +--------+--------+--------+--------+--------+ # # Process finished with exit code 0
使用grid模式的打印的數(shù)據(jù)記錄就是宮格形式很便于查找日志中的數(shù)據(jù)記錄,也是我們經(jīng)常在日志記錄中使用的一種打印方法。
接下來我們隨便選擇一種模式再次進(jìn)行打印,這里就選擇html模式來看看效果吧,這種模式之前沒有使用過我很好奇它會(huì)打印出什么樣的效果。
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'班級(jí)', u'成績(jī)', u'表現(xiàn)'] # 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;"> 班級(jí)</th><th style="text-align: right;"> 成績(jī)</th><th style="text-align: right;"> 表現(xiàn)</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>
從打印結(jié)果可以看出來了,使用html模式的打印時(shí)實(shí)際上是生成html的源碼,這還是很智能的包括style的樣式屬性也填充了。
到此這篇關(guān)于Python tabulate結(jié)合loguru打印出美觀方便的日志記錄的文章就介紹到這了,更多相關(guān)Python tabulate loguru打印日志記錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python利用pywin32實(shí)現(xiàn)自動(dòng)操作電腦
在windows系統(tǒng)上,重復(fù)性的操作可以用Python腳本來完成,其中常用的模塊是win32gui、win32con、win32api,要使用這三個(gè)模塊需要先安裝pywin32。本文就為大家介紹了如何利用這些模塊實(shí)現(xiàn)自動(dòng)操作電腦,感興趣的可以了解一下2022-11-11Python 結(jié)巴分詞實(shí)現(xiàn)關(guān)鍵詞抽取分析
這篇文章主要介紹了Python 結(jié)巴分詞實(shí)現(xiàn)關(guān)鍵詞抽取分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10python3連接mysql獲取ansible動(dòng)態(tài)inventory腳本
Ansible Inventory 是包含靜態(tài) Inventory 和動(dòng)態(tài) Inventory 兩部分的,靜態(tài) Inventory 指的是在文件中指定的主機(jī)和組,動(dòng)態(tài) Inventory 指通過外部腳本獲取主機(jī)列表。這篇文章主要介紹了python3連接mysql獲取ansible動(dòng)態(tài)inventory腳本,需要的朋友可以參考下2020-01-01python pandas dataframe如何獲取除了指定列以外的所有列
這篇文章主要介紹了python pandas dataframe如何獲取除了指定列以外的所有列問題,具有很好的參考價(jià)值,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09