我正在使用xlrd
来处理Excel文件。我在包含许多文件的文件夹上运行脚本,我正在打印与文件相关的消息。但是,对于我运行的每个文件,我也会收到以下xlrd生成的错误消息:
WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero
有没有办法抑制此错误消息,因此CLI只会打印我想要的消息?
答案 0 :(得分:11)
John的答案有效,但有一个小问题:
xlrd将该警告消息和以下换行符分别写入日志文件。因此,如果使用John提出的过滤器类,则将在stdout中获取空行而不是消息。您不应该简单地过滤掉日志输出中的所有换行符,因为可能会有“真实”警告,然后会丢失换行符。
如果你想简单地忽略xlrd的所有日志输出,这可能是最简单的解决方案:
book = xlrd.open_workbook("foo.xls", logfile=open(os.devnull, 'w'))
答案 1 :(得分:8)
查看xlrd docs的相关部分。 open_workbook
函数的第二个arg是logfile
,它应该是一个打开的文件对象或类似行为。它需要支持的只是write
方法。它默认为sys.stdout
。
所以,这样的事情(未经测试)应该可以胜任:
class MyFilter(object):
def __init__(self, mylogfile=sys.stdout):
self.f = mylogfile
def write(self, data):
if "WARNING *** OLE2 inconsistency" not in data:
self.f.write(data)
#start up
log = open("the_log_file.txt", "w")
log_filter = MyFilter(log)
book = xlrd.open_workbook("foo.xls", logfile=log_filter)
# shut down
log.close()
# or use a "with" statement
更新以回应@DaniloBargen的回答:
分别编写换行符不是xlrd
,而是Python print
语句/函数。这个脚本:
class FakeFile(object):
def write(self, data):
print repr(data)
ff = FakeFile()
for x in "foo bar baz".split():
print >> ff, x
为所有Pythons 2.2到2.7生成此输出,包括:
'foo'
'\n'
'bar'
'\n'
'baz'
'\n'
适当的现代化脚本(作为函数而不是语句打印)产生2.6,2.7,3.1,3.2和3.3的相同输出。您可以使用更复杂的过滤器类来解决此问题。以下示例还允许检查一系列短语:
import sys, glob, xlrd
class MyFilter(object):
def __init__(self, mylogfile=sys.stdout, skip_list=()):
self.f = mylogfile
self.state = 0
self.skip_list = skip_list
def write(self, data):
if self.state == 0:
found = any(x in data for x in self.skip_list)
if not found:
self.f.write(data)
return
if data[-1] != '\n':
self.state = 1
else:
if data != '\n':
self.f.write(data)
self.state = 0
logf = open("the_log_file.txt", "w")
skip_these = (
"WARNING *** OLE2 inconsistency",
)
try:
log_filter = MyFilter(logf, skip_these)
for fname in glob.glob(sys.argv[1]):
logf.write("=== %s ===\n" % fname)
book = xlrd.open_workbook(fname, logfile=log_filter)
finally:
logf.close()
答案 2 :(得分:0)
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
- > http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
答案 3 :(得分:0)
为了它的价值我得到了相同的警告信息;当我删除第一行(它是空的)时,警告消失了。
答案 4 :(得分:0)
基于John Machin's answer,下面是一些可以与sys.stdout
一起使用的工作代码(经过Python 3.6测试):
import io
import sys
import xlrd
path = "somefile.xls" # path to an XLS file to load or a file-like object for one
encoding_override = None
# Mute xlrd warnings for OLE inconsistencies
class LogFilter(io.TextIOWrapper):
def __init__(self, buffer=sys.stdout, *args, **kwargs):
self.buffer = buffer
super(LogFilter, self).__init__(buffer, *args, **kwargs)
def write(self, data):
if isinstance(data, str):
if not data.startswith("WARNING *** OLE2 inconsistency: "):
super(LogFilter, self).write(data)
elif isinstance(data, bytes):
super(LogFilter, self).write(data.decode(self.buffer.encoding))
else:
super(LogFilter, self).write(data)
def open_workbook(file_contents, encoding_override):
logfilter = LogFilter()
return xlrd.open_workbook(file_contents=file_contents, logfile=logfilter,
encoding_override=encoding_override)
if hasattr(path, 'read'):
book = open_workbook(file_contents=path.read(), encoding_override=encoding_override)
else:
with open(path, 'rb') as f:
book = open_workbook(file_contents=f.read(), encoding_override=encoding_override)