【发布时间】: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 机器,如果相关的话。
还有其他人遇到过这个问题吗?关于如何进行的任何建议?
【问题讨论】: