使用pd.read_excel读取xls文件报错:需手动指定引擎
解决Excel文件读取格式无法识别的问题
问题重现
运行以下代码时出现格式识别错误:
import pandas as pd from datetime import * import os print('Reading Sheets') df_ccc_lll_22 = pd.read_excel(r'U:\OOO\CCCO\PPP CCC HHH.xls')
报错信息:
Exception has occurred: ValueError Excel file format cannot be determined, you must specify an engine manually. File "U:\OOO\CCCO\teste.py", line 15, in <module> df_ccc_lll_22 = pd.read_excel(r'U:\OOO\CCCO\PPP CCC HHH.xls') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: Excel file format cannot be determined, you must specify an engine manually.
尝试openpyxl引擎提示非Zip格式,xlrd引擎也无法工作。
解决方案
1. 确认文件真实格式与完整性
- 手动打开该
.xls文件,检查是否能正常打开:- 如果提示文件损坏,用Excel自带的「打开并修复」功能处理,修复后另存为标准Excel格式再尝试读取。
- 如果能打开但实际是CSV/其他格式(只是后缀改成了xls),直接另存为
.xls或.xlsx格式。
2. 匹配正确的读取引擎
根据文件实际格式选择对应引擎:
- 旧版Excel格式(.xls,97-2003):
确保安装了支持xls的xlrd版本(2.0.1及以上版本仅支持xls,不支持xlsx),然后指定引擎读取:# 先安装xlrd:pip install xlrd==2.0.1 df_ccc_lll_22 = pd.read_excel(r'U:\OOO\CCCO\PPP CCC HHH.xls', engine='xlrd') - 新版Excel格式(实际是.xlsx但后缀错误):
使用openpyxl引擎读取,或直接修改文件后缀为.xlsx:# 先安装openpyxl:pip install openpyxl df_ccc_lll_22 = pd.read_excel(r'U:\OOO\CCCO\PPP CCC HHH.xls', engine='openpyxl')
3. 检查文件路径格式
尝试替换路径分隔符,避免转义问题:
# 使用正斜杠 df_ccc_lll_22 = pd.read_excel(r'U:/OOO/CCCO/PPP CCC HHH.xls', engine='xlrd') # 或双反斜杠 df_ccc_lll_22 = pd.read_excel('U:\\OOO\\CCCO\\PPP CCC HHH.xls', engine='xlrd')
内容的提问来源于stack exchange,提问作者Rodrigo Araujo Mendes
相关产品推荐
相关产品推荐

