pandas 无法打开Excel(.xlsx)文件 [英] Pandas cannot open an Excel (.xlsx) file

查看:90
本文介绍了 pandas 无法打开Excel(.xlsx)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面查看我的代码:

Please see my code below:

import pandas
df = pandas.read_excel('cat.xlsx')

运行该命令后,它给了我以下错误:

After running that, it gives me the following error:

Traceback (most recent call last):
  File "d:\OneDrive\桌面\practice.py", line 4, in <module>
    df = pandas.read_excel('cat.xlsx')
  File "D:\python\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "D:\python\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "D:\python\lib\site-packages\pandas\io\excel\_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "D:\python\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

我尝试使用pip命令卸载并重新安装Pandas.错误仍然存​​在.我安装了xlrd 2.0.1和Pandas 1.1.5.

I tried uninstall and reinstall Pandas with the pip command. The error persists. I have xlrd 2.0.1 and Pandas 1.1.5 installed.

推荐答案

发布推文并在文档]的首页上显示为橙色的大警告,而较少的橙色但仍然存在于自述文件中的自述文件

As noted in the release email, linked to from the release tweet and noted in large orange warning that appears on the front page of the documentation, and less orange but still present in the readme on the repo and the release on pypi:

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

这是由于与使用 xlrd 1.2版或更早版本来读取 .xlsx 文件有关的潜在安全漏洞.

This is due to potential security vulnerabilities relating to the use of xlrd version 1.2 or earlier for reading .xlsx files.

对于您而言,解决方案是:

In your case, the solution is to:

  • make sure you are on a recent version of pandas, at least 1.0.1, and preferably the latest release.
  • install openpyxl: https://openpyxl.readthedocs.io/en/stable/
  • change your pandas code to be:
pandas.read_excel('cat.xlsx', engine='openpyxl')

当前,熊猫> = 1.2解决了此问题.(发行说明)

Currently, pandas >= 1.2 addresses this issue. (Release Notes)

这篇关于 pandas 无法打开Excel(.xlsx)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆