读取xlsx文件错误:xlrd.biffh.XLRDError: Excel xlsx file; not supported
利用Python库xlrd中的xlrd.open_workbook()函数读取自定义xlsx表格文件时出错如下:
Traceback (most recent call last):
File "C:/Users/llll/PycharmProjects/pythonProject1/RandomForestRegression.py", line 96, in <module>
x_train , y_train , x_test , y_test = load_data(2,60,1,9,0,r'C:\Users\llll\Desktop\特征表.xlsx')
File "C:/Users/llll/PycharmProjects/pythonProject1/RandomForestRegression.py", line 14, in load_data
workbook = xlrd.open_workbook(str(FilePath)) #excel路径
File "C:\Users\llll\PycharmProjects\pythonProject1\venv\lib\site-packages\xlrd\__init__.py", line 170, in open_workbook
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported
Process finished with exit code 1
经过查资料总结后得到如下解法:
(1)检查第三方库xlrd的版本:
我这里的版本为xlrd2.0.1最新版本,问题就出在这里,需要卸载最新版本,安装旧版本,卸载安装过程如下。
PyCharm查看版本、添加与移除第三方库的方法:
●File-Settings
●Project-Python Interpreter
●移除操作:选中需要删除的包并点减号
●添加操作:点击加号
●搜索框中搜索:
●找到并点击需要安装的包,Install Package:
按照上述步骤卸载xlrd后再安装xlrd2后,错误解决。
(2)别忘了修改import名称与调用的包名称
来源:Erick T