gpt4 book ai didi

python - 使用python导入带有 Pandas 的excel

转载 作者:行者123 更新时间:2023-12-04 16:25:28 27 4
gpt4 key购买 nike

我是 Python 新手,遇到了问题。我正在尝试使用 pandas 导入一个 excel 文件,并且我正在使用 VS Code。

import pandas as pd

data = pd.read_excel(r'D:\Python\user.xlsx')
print(data)

我已经安装了 xlrd,但是我得到了这个错误:

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

有人可以帮帮我吗?

最佳答案

正如其他成员所指出的,xlrd 已停止支持 .xls 以外的格式。

推荐使用另一个库,例如 openpyxl .

正如用户 Chris Withers 在他的 answer 中所建议的那样:

  1. 安装openpyxl:pip install openpyxl

  2. 将您的代码实现为:

    import pandas as pd

    data = pd.read_excel(r'D:\Python\user.xlsx', engine='openpyxl')

    print(data)

希望这对你有用!

关于python - 使用python导入带有 Pandas 的excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65304203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com