我正在尝试将日志记录应用于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))
答案 0 :(得分:1)
当前显示日志记录是活动的还是非活动的。
我猜您是说,当您更改level
的参数logging.basicConfig
时,结果不会随之改变。我也是这样检查logging - Doc和Logging basicConfig not creating log file when i run in pycharm?之后,我可以提出一个解决方案:
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