读取Excel档案时出现Pandas和Xlrd错误 [英] Pandas and xlrd error while reading excel files

查看:123
本文介绍了读取Excel档案时出现Pandas和Xlrd错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个Python脚本,该脚本用于从Excel文件创建Pandas数据框.在过去的几天中,Pandas方法与常用的 pd.read_excel()方法完美配合.

I've been working on a Python script that deals with creating Pandas data frames from Excel files. For the past few days, the Pandas method worked perfectly with the usual pd.read_excel() method.

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

Today I've been trying to run the same code, but am running into errors. I've tried using the following code on a small test document (just two columns, 5 rows with simple integers):

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安装都是最新的.

I get the exact same issue when trying to load excel files with xlrd directly. I've tried with several different excel files, and all of my pip installations are up-to-date.

pd.read_excel 上次运行正常以来,我尚未对系统进行任何更改(我确实重新启动了系统,但未进行任何更新).如果相关,我正在使用Windows 10计算机.

I haven't made any changes to my system since pd.read_excel was last working perfectly (I did reboot my system, but it didn't involve any updates). I'm using a Windows 10 machine, if that's relevant.

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

Has anyone else had this issue? Any advice on how to proceed?

推荐答案

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

There can be many different reasons that cause this error, but you should try add engine='xlrd' or other possible values (mostly "openpyxl"). It may solve your issue, as it depends more on the excel file rather then your code.

此外,尝试将完整路径添加到文件中,而不要添加相对路径.

Also, try to add full path to the file instead of relative one.

这篇关于读取Excel档案时出现Pandas和Xlrd错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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