【python报错总结】pandas打开Excel文件失败

情景再现

import pandas as pd

df = pd.read_excel(./docs/test.xlsx)
print(通过Excel表格创建DataFrame:
, df)

控制台

Traceback (most recent call last):
  File "D:/workspace/PythonWorkspace/Demo/create_DataFrame.py", line 25, in <module>
    df = pd.read_excel(./docs/test.xlsx)
  File "D:Program FilesPythonlibsite-packagespandasutil\_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "D:Program FilesPythonlibsite-packagespandasioexcel\_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "D:Program FilesPythonlibsite-packagespandasioexcel\_base.py", line 1131, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "D:Program FilesPythonlibsite-packagespandasioexcel\_xlrd.py", line 24, in __init__
    import_optional_dependency("xlrd", extra=err_msg)
  File "D:Program FilesPythonlibsite-packagespandascompat\_optional.py", line 109, in import_optional_dependency
    raise ImportError(msg) from None
ImportError: Missing optional dependency xlrd. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

错误的提示信息说明,我们当前缺失‘xlrd’依赖,需要安装高于或等于1.0.0版本的。

安装依赖(xlrd)

我们在Terminal中输入以下命令

D:workspacePythonWorkspace> pip3 install xlrd
Collecting xlrd
  Using cached https://files.pythonhosted.org/packages/a6/0c/c2a72d51fe56e08a08acc85d13013558a2d793028ae7385448a6ccdfae64/xlrd-2.0.1-py2.py3-none-any.whl
Installing collected packages: xlrd
Successfully installed xlrd-2.0.1
You are using pip version 10.0.1, however version 22.1b1 is available.
You should consider upgrading via the python -m pip install --upgrade pip command.

此时,再次运行程序

Traceback (most recent call last):
  File "D:/workspace/PythonWorkspace/Demo/create_DataFrame.py", line 25, in <module>
    df = pd.read_excel(./docs/test.xlsx)
  File "D:Program FilesPythonlibsite-packagespandasutil\_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "D:Program FilesPythonlibsite-packagespandasioexcel\_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "D:Program FilesPythonlibsite-packagespandasioexcel\_base.py", line 1103, in __init__
    f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, "
ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

会发现,还是报错了。。。

错误提示信息:当前安装的xlrd依赖是2.0.1版本的,当xlrd>=2.0版本时,仅支持xls格式的文件打开;也可以使用openpyxl依赖进行替代

当前有两种思路:

    将需要打开的文件另存为xls格式的文件 安装openpyxl依赖
D:workspacePythonWorkspace> pip3 install openpyxl
Collecting openpyxl
  Using cached https://files.pythonhosted.org/packages/1c/a6/8ce4d2ef2c29be3235c08bb00e0b81e29d38ebc47d82b17af681bf662b74/openpyxl-3.0.9-py2.py3-none-any.whl
Requirement already satisfied: et-xmlfile in d:program filespythonlibsite-packages (from openpyxl) (1.1.0)
Installing collected packages: openpyxl
Successfully installed openpyxl-3.0.9

再次运行程序,发现可以正常打开Excel文件了

通过Excel表格创建DataFrame:
   衣服类型   成本   售价   销量
0  羽绒服  180  300  100
1  家居服  120  250  200
2  牛仔裤   80  199  300
经验分享 程序员 微信小程序 职场和发展