我正在提取电子表格并意识到有些会受密码保护,因此下面的代码无法读取某些电子表格,这很好。我正试图通过except行传递这些电子表格,我从zipfile导入了BadZipfile,试图将它们识别为错误。
但是,except函数不会将BadZipfile识别为错误,并且仍然会在Traceback中引发错误(如下所示)。我希望通过将except行留空,它会传递任何错误并再次循环以在剩余文件上运行try语句,但是它似乎不会将BadZipfile识别为错误。
在except语句中也需要KeyError,因为有些电子表格没有我想要提取的索引标签。也许我应该通过任何例外,因为我没有循环的文件可能会出现更多错误。我更愿意处理在此时工作的任何电子表格,并在稍后协调错误。
因此,如何确保BadZipfile被识别为错误,然后使用except语句传递任何错误?
CODE
import itertools
import glob
from openpyxl import load_workbook
from pandas import DataFrame
import pandas as pd
import os
from zipfile import BadZipfile
def get_data(ws):
for row in ws.values:
row_it = iter(row)
for cell in row_it:
if cell is not None:
yield itertools.chain((cell,), row_it)
break
def read_workbook(file_):
wb = load_workbook(file_, data_only=True)
for sheet in wb.worksheets:
ws = sheet
return DataFrame(get_data(ws))
path =r'dir'
allFiles = glob.glob(path + "/*.xlsx")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
parsed_file = read_workbook(file_)
parsed_file['filename'] = os.path.basename(file_)
parsed_file.set_index(parsed_file.columns[0], inplace = True)
parsed_file.index.str.strip()
try:
parsed_file.loc["Staff" : "Total Staff"].copy()
list_.append(parsed_file)
except KeyError or BadZipfile:
pass
frame = pd.concat(list_)
print(frame.dropna(axis='columns', thresh=2, inplace = True))
错误
Traceback (most recent call last):
File "<ipython-input-47-2d5508ddf805>", line 1, in <module>
runfile('C:dir.py', wdir='C:dir')
File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:dir.py", line 35, in <module>
parsed_file = read_workbook(file_)
File "C:dir.py", line 25, in read_workbook
wb = load_workbook(file_, data_only=True)
File "C:\ProgramData\Anaconda2\lib\site-packages\openpyxl\reader\excel.py", line 164, in load_workbook
archive = _validate_archive(filename)
File "C:\ProgramData\Anaconda2\lib\site-packages\openpyxl\reader\excel.py", line 121, in _validate_archive
archive = ZipFile(f, 'r', ZIP_DEFLATED)
File "C:\ProgramData\Anaconda2\lib\zipfile.py", line 770, in __init__
self._RealGetContents()
File "C:\ProgramData\Anaconda2\lib\zipfile.py", line 813, in _RealGetContents
raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file
答案 0 :(得分:4)
首先,检查堆栈跟踪。你错误被抛出的地方没有except (KeyError, BadZipFile):
。
第二,为了捕获多种类型的错误,您需要except KeyError or BadZipFile:
而不是except KeyError
,它只评估为or
(了解Name,Channel0,Channel1,Channel2,Channel3,Channel4
CSBA10,125833,147883,184269,162270,151366
CSBA20,125533,145883,154269,152270,155365
如何工作)
答案 1 :(得分:2)
来自docs page on error handling:
try:
# your code
except (KeyError, BadZipfile):
pass
如果您想对错误做些什么,可以使用
try:
# your code
except (KeyError, BadZipfile) as err:
# do something
当您调用语句read_workbook
时,您的load_workbook
函数实际上发生了错误,因此您在发生错误时无法捕获错误。