关于xlrd.biffh.XLRDError: Excel xlsx file; not supported的解决方法
关于xlrd.biffh.XLRDError: Excel xlsx file; not supported的解决方法
问题描述
在做机器学习实验时,需要将iris_train.xlsx等文件读入作为数据集,在运行代码:
import xlrd as xd
file_train_path = 'data/iris_train.xlsx'
file_train_xlsx = xd.open_workbook(file_train_path)
file_train_sheet = file_train_xlsx.sheet_by_name('Sheet1')
x_train = []
y_train = []
for row in range(file_train_sheet.nrows):
x_data = []
for col in range(file_train_sheet.ncols):
if col < file_train_sheet.ncols-1:
x_data.append(file_train_sheet.cell_value(row, col))
else:
y_train.append(file_train_sheet.cell_value(row, col))
x_train.append(list(x_data))
print(x_train)
print(y_train)
有一行代码出现了问题:
file_train_xlsx = xd.open_workbook(file_train_path)
发生错误,报错信息如下:
报错信息:file_train_xlsx = xd.open_workbook(file_train_path)
报错原因
xlrd包的版本过高(2.0.1),不支持xlsx格式,只支持xls格式。
解决方法
- 调低xlrd包的版本,改为1.2.0
打开设置-Python解释器,双击xlrd包,选择指定版本下载。
- 把xlsx文件改为xls文件
结果
调低xlrd包的版本后,程序能顺利运行: