【问题标题】:Reading an Excel file and ImportError for xlrd in Jupyterlab在 Jupyterlab 中为 xlrd 读取 Excel 文件和 ImportError
【发布时间】:2020-12-08 14:34:59
【问题描述】:

类似的问题想必在这里讨论过很多,但似乎并没有完全解决,所以我想更新讨论。

我最近开始使用 Python,并尝试在 Jupyterlab (2.2.9) 中使用

读取 Excel 文件
import pandas as pd
sample_file = pd.read_excel("sample.xlsx")
sample_file.head()

然后我收到如下所述的错误。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-8-cfbece5b87d5> in <module>
----> 1 sample_data = pd.read_excel("sample_data.xlsx")
      2 sample_data.head()

~/.pyenv/versions/3.8.2/lib/python3.8/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)
    302 
    303     if not isinstance(io, ExcelFile):
--> 304         io = ExcelFile(io, engine=engine)
    305     elif engine and engine != io.engine:
    306         raise ValueError(

~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pandas/io/excel/_base.py in __init__(self, io, engine)
    822         self._io = stringify_path(io)
    823 
--> 824         self._reader = self._engines[engine](self._io)
    825 
    826     def __fspath__(self):

~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py in __init__(self, filepath_or_buffer)
     18         """
     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 

~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pandas/compat/_optional.py in import_optional_dependency(name, extra, raise_on_missing, on_version)
     90     except ImportError:
     91         if raise_on_missing:
---> 92             raise ImportError(msg) from None
     93         else:
     94             return None

ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

我用的是Anaconda,xlrd的版本已经是1.2.0了。以防万一我已经这样做了

conda install xlrd

正如这里许多人的建议(我也尝试用 pip 替换 conda)。但是,仍然会发生相同的错误。我完全迷路了。

【问题讨论】:

    标签: python excel anaconda jupyter-lab xlrd


    【解决方案1】:
    import pandas as pd
    sample_file = pd.read_excel("sample.xlsx")
    sample_file.head()
    

    试试这个

    【讨论】:

    • 抱歉,我打错字了,应该加上“pd”。在 read_excel 之前。
    【解决方案2】:

    恐怕熊猫的建议有点过时了。您需要:

    1. conda install openpyxl
    2. 重启 Jupyterlab。
    3. 将您的熊猫代码更改为pd.read_excel("sample.xlsx", engine='openpyxl')

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 2021-02-02
      • 2021-07-12
      • 2012-05-14
      • 1970-01-01
      • 2011-02-13
      • 2021-05-03
      • 1970-01-01
      相关资源
      最近更新 更多