栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 情景再现
import pandas as pd

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

控制台

Traceback (most recent call last):
  File "D:/workspace/PythonWorkspace/Demo/create_DataFrame.py", line 25, in 
    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 
    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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号