Pandas读取Excel报错:'ElementTree'对象无'getiterator'属性求助
Fixing AttributeError When Reading Excel with Pandas in Python 3.9+
Hey there! Let's break down why you're hitting this error and fix it step by step.
What's Causing the Issue?
The AttributeError: 'ElementTree' object has no attribute 'getiterator' pops up for two key reasons:
- You’re using Python 3.9 or newer, where the
xml.etree.ElementTree.ElementTreeclass removed the deprecatedgetiterator()method. - Your installed version of
xlrdis outdated and still relies on this removed method. On top of that, xlrd 2.0.0 and later no longer support.xlsxfiles—they only work with older.xlsformats.
Solution 1: Use Openpyxl as the Engine (Recommended)
Since you already have openpyxl installed, just specify it as the engine in pd.read_excel(). This is the most reliable fix for .xlsx files:
import pandas as pd filepath = r'/Users/vignesh/Desktop/Python/test2.xlsx' df = pd.read_excel(filepath, engine='openpyxl') print(df)
Solution 2: Double-Check File Path & Dependencies
Make sure these details are correct to avoid other hiccups:
- Verify your file path: In your error trace, you called
pd.read_excel('test2.xlsx')(relative path) but your code uses an absolute path. Ensure the file exists in the location you’re referencing. - Update
openpyxlto the latest version: Run this command in your terminal to confirm:pip install --upgrade openpyxl
Bonus: For .xls Files (If Applicable)
If your file was an older .xls format, you could install a Python 3.9-compatible version of xlrd that supports .xls:
pip install xlrd==1.2.0
But skip this if you’re working with .xlsx—stick with Solution 1 instead.
内容的提问来源于stack exchange,提问作者user40143
相关产品推荐
相关产品推荐

