English 中文(简体)
页: 1
原标题:Python xlrd: suppress warning messages
  • 时间:2011-10-01 09:19:29
  •  标签:
  • python
  • xlrd

我正在使用<代码>xlrd处理Excel文档。 我正在用一个文件夹的文字,其中载有许多文件,我正在印刷有关文件的信息。 然而,每份档案中,我也收到以下Xlrd生成的错误信息:

WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero

是否有办法压制这一错误信息,因此,国家扫盲委员会只能印制我想要的信息?

最佳回答

https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#_init_.open_workbook-Function”rel=“noretinger”>xlrd docs 。 <代码>开放式——工作手册功能的二倍增是<编码>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

<>,对@Danilo Bargen的答复:

它没有<条码>xlrd,单独书写新线,而是打上了<条码>印/条码>的说明/功能。 原文:

class FakeFile(object):
    def write(self, data):
        print repr(data)

ff = FakeFile()
for x in "foo bar baz".split():
    print >> ff, x

生产所有第2.2至2.7 Python的产量:

 foo 
 
 
 bar 
 
 
 baz 
 
 

适当现代化的文字(作为功能而不是说明的印本)在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] !=  
 :
                self.state = 1
        else:
            if data !=  
 :
                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 ===
" % fname)
        book = xlrd.open_workbook(fname, logfile=log_filter)
finally:
    logf.close()
问题回答

John的回答是个小问题:

xlrd将这一警告信息与下列新线特性分别与记录单进行书写。 因此,如果你使用约翰提议的过滤类别,你将用空线而不是电文。 你们不要简单地从原木产出中抽出所有新线,因为可能出现“真实”警告,然后会遗漏新线。

如果你想简单地忽视Xlrd的所有原产地,这可能是最简单的解决办法:

book = xlrd.open_workbook("foo.xls", logfile=open(os.devnull,  w ))

如果你把这一警告改为pandas_excel功能,那么你可以这样做:

import os
import xlrd
import pandas as pd

workbook = xlrd.open_workbook(your_path, logfile=open(os.devnull, "w"))
df = pd.read_excel(workbook) # read_excel accepts workbooks too

对于我所珍惜的同样警告,当我删除第一行(空洞)时,警告就消失了。

依据,John Machin swer,这里是一些工作守则(用Ad 3.6测试),该守则使用sys.stdout:

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)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签