python隐藏程序输出_Python隐藏warning和日志输出

最近跑程序时经常会出现一些warning或者第三方库的一些日志输出,这些东西有时会影响到其他程序对Python脚本的输出的读取。下面记录遇到的几种情况和对应的解决方案。

jieba 的日志输出

使用jieba的时候,会遇到以下情况:

Building prefix dict from the default dictionary ...

Loading model from cache C:\Users\hello\AppData\Local\Temp\jieba.cache

Loading model cost 0.888 seconds.

Prefix dict has been built succesfully.

这种情况,把日志关闭就可以了。

logging日志有5种级别,

从低到高是

1 DEBUG

2 INFO

3 WARNING

4 ERROR

5 CRITICAL

这里只要比debug的级别高,就可以屏蔽debug级别的输出。

import logging

jieba.setLogLevel(logging.INFO)

gensim warning

使用gensim的时候遇到了以下两种警告

C:\Users\hello\AppData\Local\Programs\Python\Python36\lib\site-packages\gensim\utils.py:1212: UserWarning: detected Windows; aliasing chunkize to chunkize_serial

warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")

C:\Users\hello\AppData\Local\Programs\Python\Python36\lib\site-packages\gensim\matutils.py:718: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int32 == np.dtype(int).type`.

if np.issubdtype(vec.dtype, np.int):

解决方案:

warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')

warnings.filterwarnings(action='ignore', category=FutureWarning, module='gensim')

关于这个方法的官方文档:

43e8900c9f1f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

tensorflow 警告

import os

os.environ["TF_CPP_MIN_LOG_LEVEL"]='1'

# 默认的显示等级,显示所有信息

os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'

# 只显示 warning 和 Error

os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'

# 只显示 Error

通常把 level设成2,就会屏蔽警告信息了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 中,可以使用标准库中的 logging 模块来实现日志输出到多个文件。下面是一个简单的示例代码,演示了如何将日志同时输出到两个不同的文件中: ```python import logging # 创建 logger 对象 logger = logging.getLogger(__name__) # 创建文件处理器,将日志输出到 file1.log 文件中 file_handler_1 = logging.FileHandler('file1.log') file_handler_1.setLevel(logging.INFO) # 创建文件处理器,将日志输出到 file2.log 文件中 file_handler_2 = logging.FileHandler('file2.log') file_handler_2.setLevel(logging.WARNING) # 设置日志格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler_1.setFormatter(formatter) file_handler_2.setFormatter(formatter) # 将处理器添加到 logger 对象中 logger.addHandler(file_handler_1) logger.addHandler(file_handler_2) # 输出日志信息 logger.debug('debug message') logger.info('info message') logger.warning('warning message') logger.error('error message') logger.critical('critical message') ``` 在上面的代码中,我们首先创建了一个 logger 对象,并为其设置了两个不同的文件处理器。然后,我们设置了日志级别和日志格式,并将处理器添加到 logger 对象中。最后,我们通过 logger 对象输出了一些日志信息。 在运行上面的代码后,会在当前目录下生成两个日志文件:file1.log 和 file2.log。其中,file1.log 中包含了所有级别的日志信息,而 file2.log 中只包含了 WARNINGERROR 和 CRITICAL 级别的日志信息。 如果需要输出到更多的日志文件,只需要创建更多的文件处理器,并将它们添加到 logger 对象中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值