簡體   English   中英

如何切換Jupyter Notebook顯示日志記錄

[英]How to toggle Jupyter Notebook display logging

我正在嘗試將日志記錄應用於Jupyter Notebook的display(df)功能。 如何編輯代碼的最后一行,以便僅在記錄INFO處於活動狀態時才顯示數據框? 當前顯示日志記錄是活動的還是非活動的。

import pandas as pd

logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')

filename = 'test.csv'
df=pd.read_csv(filename, index_col=None, encoding='utf-8')

logging.info(display(df))

當前顯示日志記錄是活動的還是非活動的。

我猜您是在說,當您更改logging.basicConfig的參數level時,結果不會隨之改變。 我也是這樣 檢查日志記錄后- 當我在pycharm中運行時, DocLogging basicConfig無法創建日志文件嗎? ,我可以提出一個解決方案:

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

logging.basicConfig(format=' %(asctime)s - %(levelname)s - %(message)s')
df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]})

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("\n"+str(df))
logger.info("\n"+str(df))
logger.warning("\n"+str(df))
logger.error("\n"+str(df))
logger.critical("\n"+str(df))

您可以使用logger.setLevel(logging.DEBUG)來查看它是否有效。

而且由於無論如何都將調用display(df) ,就像@AChampion所說的那樣,我使用"\\n"+str(df)代替display(df)

輸出:

 2019-01-18 14:20:47,710 - DEBUG - 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
 2019-01-18 14:20:47,715 - INFO - 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
 2019-01-18 14:20:47,720 - WARNING - 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
 2019-01-18 14:20:47,724 - ERROR - 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
 2019-01-18 14:20:47,728 - CRITICAL - 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM