【问题标题】:Pandas and xlrd error while reading excel files读取 excel 文件时出现 Pandas 和 xlrd 错误
【发布时间】:2021-02-02 13:01:17
【问题描述】:

我一直在编写一个 Python 脚本,该脚本处理从 Excel 文件创建 Pandas 数据框。在过去的几天里,Pandas 方法与通常的 pd.read_excel() 方法完美配合。

今天我一直在尝试运行相同的代码,但遇到了错误。我尝试在一个小型测试文档上使用以下代码(只有两列,5 行,带有简单的整数):

import pandas as pd

pd.read_excel("tstr.xlsx")

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\xlrd\__init__.py", line 130, in open_workbook
    bk = xlsx.open_workbook_2007_xml(
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\xlrd\xlsx.py", line 812, in open_workbook_2007_xml
    x12book.process_stream(zflo, 'Workbook')
  File "C:\Users\micro\AppData\Local\Programs\Python\Python39\lib\site-packages\xlrd\xlsx.py", line 266, in process_stream
    for elem in self.tree.iter() if Element_has_iter else self.tree.getiterator():
AttributeError: 'ElementTree' object has no attribute 'getiterator'

尝试直接使用xlrd 加载 excel 文件时,我遇到了完全相同的问题。我尝试了几个不同的 excel 文件,我所有的 pip 安装都是最新的。

自从pd.read_excel 上次正常运行以来,我没有对我的系统进行任何更改(我确实重新启动了我的系统,但它没有涉及任何更新)。我使用的是 Windows 10 机器,如果相关的话。

还有其他人遇到过这个问题吗?关于如何进行的任何建议?

【问题讨论】:

标签: python excel pandas xlrd


【解决方案1】:

导致此错误的原因可能有很多,但您应该尝试添加engine='xlrd' 或其他可能的值(主要是“openpyxl”)。它可能会解决您的问题,因为它更多地取决于 excel 文件而不是您的代码。

另外,尝试添加文件的完整路径而不是相对路径。

【讨论】:

  • 设置engine='openpyxl' 似乎已经解决了这个问题。设置完整路径也很聪明——我通常这样做,但在示例中使用了更简单的字符串。非常感谢!
  • 由于“excel文件”的类型不同,所以有不同的解决方案。查看python-excel.org 了解更多详情。 @Jordan,感谢您为我们提供了对您有用的答案。在这里提到这两个引擎对其他人有帮助!
【解决方案2】:

openpyxl.utils.exceptions.InvalidFileExceptionopenpyxl不支持旧的.xls文件格式,请使用xlrd阅读文件,或将其转换为更新的 .xlsx 文件格式。

所以对我来说这个论点:

  • engine="xlrd" 工作于 .xls
  • engine="openpyxl" 工作于 .xlsx

【讨论】:

    【解决方案3】:

    这对我有用

    #Back to linux prompt and install openpyxl
    
    pip install openpyxl
    
    #Add engine='openpyxl' in the python argument
    
    data = pd.read_excel(path, sheet_name='Sheet1', parse_dates=True, engine='openpyxl')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 2023-04-07
      • 2012-09-24
      • 2021-05-03
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多