pandas - XLRDError : Unsupported format, 或损坏的文件

标签 pandas xlsx xlrd

使用 pandas 读取 .xlsx 文件时出现错误。看起来它正在打开文件,因为它能够读取列名的前 8 个字符,即 员工 ID,但因此错误而失败。我看到很多关于此的帖子,但最后一部分从来​​都不是这些错误消息中的列名称。有什么建议吗?

在开发环境中,当我以 Excel 形式打开此文件并重新加载到服务器时,它起作用了。

错误:XLRDError:格式不受支持,或文件损坏:需要 BOF 记录;找到“员工”

最佳答案

release email 中所述,链接至 release tweet并在documentation首页上出现的大橙色警告中注明,橙色较少,但仍然存在于 readme on the repo 中和 release on pypi :

xlrd 已明确删除对 xls 文件以外的任何内容的支持。

这是由于使用 xlrd 1.2 或更早版本读取 .xlsx 文件时存在潜在的安全漏洞。

可用的解决方案:

  • 指定较旧的 xlrd 版本,即 xlrd==1.2.0 或
  • 在 pandas 上使用 openpyxl:

确保您使用的是最新版本的 pandas,至少为 1.0.1,并且最好是最新版本。 安装 openpyxl:https://openpyxl.readthedocs.io/en/stable/ 将您的 pandas 代码更改为:

pandas.read_excel('cat.xlsx', engine='openpyxl')

下一个 pandas 版本 pandas 1.2 将默认执行此操作。

关于pandas - XLRDError : Unsupported format, 或损坏的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66469030/

相关文章:

python - 强制 Selenium 暂停并等待字段可用

python - 将 DataFrame 过滤为重复项并计算结果的分组均值

pandas - 向特定数据框列添加单个值

excel - 适用于 Excel 的 Apache POI : Setting the cell type to "text" for an entire column

r - 如何修复 OutOfMemoryError (Java) : GC overhead limit exceeded in r?

excel - 使用 xlrd/xlutils 复制工作表

Python Excel 模板读取和重写,维护公式和格式

python - 从稀疏日期时间索引获取范围

python - 'DataFrame' 对象没有属性 'sort'

r - 在 R 中创建 xlsx 工作表 - 字体颜色不起作用