Python xlrd: suppress warning messages

前端 未结 5 1815
暖寄归人
暖寄归人 2021-01-02 10:19

I am using xlrd to process Excel files. I am running a script on a folder that contains many files, and I am printing messages related to the files. However, fo

5条回答
  •  情深已故
    2021-01-02 10:38

    The answer by John works, but has a small problem:

    xlrd writes that warning message and the following newline character separately to the logfile. Therefore you will get an empty line in your stdout instead of the message, if you use the filter class proposed by John. You shouldn't simply filter out all newlines from the log output either though, because there could be "real" warnings that would then be missing the newlines.

    If you want to simply ignore all log output by xlrd, this is probably the simplest solution:

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

提交回复
热议问题