Python:ValueError和XLRDError的异常处理

2024-05-12 19:18:30 发布

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

我试图读取一个excel文件,其中sheetname输出不存在。在这种情况下,我想排除作为ValueError和XLRDError出现的错误

使用以下选项不起作用。我们是否需要指定两个错误?是否有其他方法

try:
    df= pd.read_excel("Sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)

except NameError:
    print ("Sheet not exists")

错误输出如下:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\xlrd\book.py in sheet_by_name(self, sheet_name)
    473         try:
--> 474             sheetx = self._sheet_names.index(sheet_name)
    475         except ValueError:

ValueError: 'Output' is not in list

During handling of the above exception, another exception occurred:

XLRDError                                 Traceback (most recent call last)
<ipython-input-126-7479be6ca02b> in <module>
      1 os.chdir(inputs_path + "/Output")
      2 try:
----> 3     df = pd.read_excel("sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)
      4 
      5 except ValueError:

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    332         convert_float=convert_float,
    333         mangle_dupe_cols=mangle_dupe_cols,
--> 334         **kwds,
    335     )
    336 

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, converters, true_values, false_values, skiprows, nrows, na_values, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    883             convert_float=convert_float,
    884             mangle_dupe_cols=mangle_dupe_cols,
--> 885             **kwds,
    886         )
    887 

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, dtype, true_values, false_values, skiprows, nrows, na_values, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    434 
    435             if isinstance(asheetname, str):
--> 436                 sheet = self.get_sheet_by_name(asheetname)
    437             else:  # assume an integer if not a string
    438                 sheet = self.get_sheet_by_index(asheetname)

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in get_sheet_by_name(self, name)
     41 
     42     def get_sheet_by_name(self, name):
---> 43         return self.book.sheet_by_name(name)
     44 
     45     def get_sheet_by_index(self, index):

~\Anaconda3\lib\site-packages\xlrd\book.py in sheet_by_name(self, sheet_name)
    474             sheetx = self._sheet_names.index(sheet_name)
    475         except ValueError:
--> 476             raise XLRDError('No sheet named <%r>' % sheet_name)
    477         return self.sheet_by_index(sheetx)
    478 

XLRDError: No sheet named <'Output'>

Tags: nameinselfconvertindexbyfloatexcel
1条回答
网友
1楼 · 发布于 2024-05-12 19:18:30

您需要从xldr库导入XLDR Error

from xlrd import XLRDError


try:
    df= pd.read_excel("Sample.xlsx",sheet_name='Output', usecols='B,W', skiprows=0)

except XLRDError:
    print ("Sheet not exists")

相关问题 更多 >