![](/img/trans.png)
[英]Python: Pandas read_excel cannot open .xls file, xlrd not supported
[英]AssertionError when calling pands read_excel on xls file using xlrd engine
我正在嘗試通過 pandas 將 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:
如果您的數據中有一個尚未設置為“日期”格式的日期列,則通常會發生這種情況。 在excel中將日期列設置為“日期”,錯誤將消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.