日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?tabulate結合loguru打印出美觀方便的日志記錄_python

作者:Python?集中營 ? 更新時間: 2022-11-21 編程語言

在開發過程中經常碰到在本地環境無法完成聯調測試的情況,必須到統一的聯機環境對接其他系統測試。往往是出現了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的樣式屬性也填充了。

原文鏈接:https://www.cnblogs.com/lwsbc/p/16773668.html

欄目分類
最近更新