使用xlrd引擎在xls文件上调用pands read_excel时出现AssertionError

时间:2020-09-10 15:24:23

标签: python excel pandas dataframe xls

我正尝试通过熊猫将Excel文件(.xls)读入数据框,如下所示:

df = pd.read_excel(
        filename, sheet_name='Sheet1', nrows=6)

很不幸,我收到一个AssertionError。但是,如果我在excel中打开文件,然后单击“保存”,然后重新运行,则可以正常运行。我没有更改任何数据,只是在Excel中打开并保存。有人遇到过这个问题吗?

这是我得到的断言错误:

AssertionError                            Traceback (most recent call last)
<ipython-input-153-58dcba1b45c3> in <module>
      1 df = pd.read_excel(
----> 2             filename, sheet_name='Sheet1', nrows=6)

~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

~\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, skip_footer, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    308 
    309     if not isinstance(io, ExcelFile):
--> 310         io = ExcelFile(io, engine=engine)
    311     elif engine and engine != io.engine:
    312         raise ValueError(

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, io, engine)
    817         self._io = _stringify_path(io)
    818 
--> 819         self._reader = self._engines[engine](self._io)
    820 
    821     def __fspath__(self):

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in __init__(self, filepath_or_buffer)
     19         err_msg = "Install xlrd >= 1.0.0 for Excel support"
     20         import_optional_dependency("xlrd", extra=err_msg)
---> 21         super().__init__(filepath_or_buffer)
     22 
     23     @property

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, filepath_or_buffer)
    357             self.book = self.load_workbook(filepath_or_buffer)
    358         elif isinstance(filepath_or_buffer, str):
--> 359             self.book = self.load_workbook(filepath_or_buffer)
    360         else:
    361             raise ValueError(

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in load_workbook(self, filepath_or_buffer)
     34             return open_workbook(file_contents=data)
     35         else:
---> 36             return open_workbook(filepath_or_buffer)
     37 
     38     @property

~\Anaconda3\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
    155         formatting_info=formatting_info,
    156         on_demand=on_demand,
--> 157         ragged_rows=ragged_rows,
    158     )
    159     return bk

~\Anaconda3\lib\site-packages\xlrd\book.py in open_workbook_xls(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
    118             bk._sheet_list = [None for sh in bk._sheet_names]
    119             if not on_demand:
--> 120                 bk.get_sheets()
    121         bk.nsheets = len(bk._sheet_list)
    122         if biff_version == 45 and bk.nsheets > 1:

~\Anaconda3\lib\site-packages\xlrd\book.py in get_sheets(self)
    721         for sheetno in xrange(len(self._sheet_names)):
    722             if DEBUG: print("GET_SHEETS: sheetno =", sheetno, self._sheet_names, self._sh_abs_posn, file=self.logfile)
--> 723             self.get_sheet(sheetno)
    724 
    725     def fake_globals_get_sheet(self): # for BIFF 4.0 and earlier

~\Anaconda3\lib\site-packages\xlrd\book.py in get_sheet(self, sh_number, update_pos)
    712             sh_number,
    713         )
--> 714         sh.read(self)
    715         self._sheet_list[sh_number] = sh
    716         return sh

~\Anaconda3\lib\site-packages\xlrd\sheet.py in read(self, bk)
   1107                     saved_obj_id = None
   1108             elif rc == XL_NOTE:
-> 1109                 self.handle_note(data, txos)
   1110             elif rc == XL_FEAT11:
   1111                 self.handle_feat11(data)

~\Anaconda3\lib\site-packages\xlrd\sheet.py in handle_note(self, data, txos)
   1985         # string length).
   1986         # Issue 4 on github: Google Spreadsheet doesn't write the undefined byte.
-> 1987         assert (data_len - endpos) in (0, 1)
   1988         if OBJ_MSO_DEBUG:
   1989             o.dump(self.logfile, header="=== Note ===", footer= " ")

AssertionError: 

0 个答案:

没有答案