Python:禁用iptcinfo警告

2024-04-29 13:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用iptcinfo Python模块从图片中获取元数据,但它给我带来了许多(无用)此类警告:

(“警告:字符集识别问题”,“\x1b'”)

这意味着什么?如何删除这些警告(或防止它们发生),因为它们似乎对我的代码没有任何意义?在

我的代码很简单:

import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg") 

Tags: 模块数据代码import警告图片意义jpg
3条回答

问题是你使用的模块做了一些你不希望模块做的事情。在

print (
       'WARNING: problems with charset recognition',
      repr(temp))

好吧,不能就这样残废。但是他们的好所以线程如何达到同样的效果。在

Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call

Suppress calls to print (python)

所以把两者结合起来

^{pr2}$

而且效果很好

No Print

This line in the code似乎正在生成警告:

LOG.warn('problems with charset recognition %s', repr(temp))

看到这个消息是因为Python日志模块的默认日志记录级别是“warning”。在

在代码中,可以将库的记录器logging level修改为更高,这样就不会看到警告:

^{pr2}$

编辑:为了进行故障排除,下面是一个片段,可以查看每个记录器的级别:

for logger_name in logging.Logger.manager.loggerDict:
    logger_level = logging.getLogger(logger_name).level
    print logger_name, logging.getLevelName(logger_level)

首先,我认为iptcinfo应该与python2完美地配合使用。在

另一个解决方案是修改原始源代码:

负责警告的原始代码

('WARNING: problems with charset recognition', "'\x1b'")

在971号线iptcinfo.py文件。在

^{pr2}$

您可以将原始的github repo分叉,然后简单地将其注释掉

#LOG.warn('problems with charset recognition %s', repr(temp))

那么

#Uninstall the original installation
pip uninstall iptcinfo
#Do pip install from your own fork. e.g.:
pip install git+git://github.com/Sulli/iptcinfo.git

相关问题 更多 >