吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2218|回复: 23
收起左侧

[已解决] pyhton调用pandas来读取excel文件并输出,结果报错

[复制链接]
王嘉伟 发表于 2022-3-10 19:04
本帖最后由 王嘉伟 于 2022-3-10 22:06 编辑

excel文件截图已经上传附件

python代码:
import pandas as pd


aaa = pd.read_excel('001.xls',sheet_name='Sheet1')

##print(df.values)
print("\n(2)2行第3列的值:")
print(aaa.values[2,1])

报错内容:
Traceback (most recent call last):
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 126, in import_optional_dependency
    module = importlib.import_module(name)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 4, in <module>
    aaa = pd.read_excel('001.xls',sheet_name='Sheet1')
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1363, in __init__
    if import_optional_dependency("xlrd", errors="ignore") is None:
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 142, in import_optional_dependency
    version = get_version(module_to_get)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 68, in get_version
    raise ImportError(f"Can't determine version for {module.__name__}")
ImportError: Can't determine version for xlrd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 4, in <module>
    aaa = pd.read_excel('001.xls',sheet_name='Sheet1')
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1419, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_xlrd.py", line 24, in __init__
    import_optional_dependency("xlrd", extra=err_msg)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 129, in import_optional_dependency
    raise ImportError(msg)
ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
微信截图_20220310190231.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

kafei000 发表于 2022-3-10 19:12
Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
自己百度,报错信息都给你了
 楼主| 王嘉伟 发表于 2022-3-10 19:15
kafei000 发表于 2022-3-10 19:12
Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to inst ...

我百度搜索了,网上让我安装xlrd模块,我之前就安装了这个模块了呀
 楼主| 王嘉伟 发表于 2022-3-10 19:16
kafei000 发表于 2022-3-10 19:12
Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to inst ...

是需要在代码里面调用xlrd模块吗
hh920081949 发表于 2022-3-10 19:35
import pandas as pd


aaa = pd.read_excel('001.xls',sheet_name='Sheet1')

##print(df.values)
print("\n(2)第2行第3列的值:")
print(aaa.iloc[2,1])
试试这个
sxfxtf 发表于 2022-3-10 19:37
王嘉伟 发表于 2022-3-10 19:15
我百度搜索了,网上让我安装xlrd模块,我之前就安装了这个模块了呀

pandas 是依赖xlrd的, 你可以删除以前的xlrd版本.重新安装pandas会自动安装覆盖的
 楼主| 王嘉伟 发表于 2022-3-10 19:44

依旧报错,显示:
Traceback (most recent call last):
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 126, in import_optional_dependency
    module = importlib.import_module(name)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 13, in <module>
    aaa = pd.read_excel('001.xls',sheet_name='Sheet1')
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1363, in __init__
    if import_optional_dependency("xlrd", errors="ignore") is None:
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 142, in import_optional_dependency
    version = get_version(module_to_get)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 68, in get_version
    raise ImportError(f"Can't determine version for {module.__name__}")
ImportError: Can't determine version for xlrd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 13, in <module>
    aaa = pd.read_excel('001.xls',sheet_name='Sheet1')
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1419, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_xlrd.py", line 24, in __init__
    import_optional_dependency("xlrd", extra=err_msg)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 129, in import_optional_dependency
    raise ImportError(msg)
ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
 楼主| 王嘉伟 发表于 2022-3-10 19:46
sxfxtf 发表于 2022-3-10 19:37
pandas 是依赖xlrd的, 你可以删除以前的xlrd版本.重新安装pandas会自动安装覆盖的

我删除了xlrd模块,重新安装的pandas模块也是依旧报错
sxfxtf 发表于 2022-3-10 19:48
王嘉伟 发表于 2022-3-10 19:46
我删除了xlrd模块,重新安装的pandas模块也是依旧报错

那i再单独安装xlrd 安装版本要大于 1.0.0
 楼主| 王嘉伟 发表于 2022-3-10 19:49

我改了一下代码,代码变更如下:
import pandas,xlwt,xlrd

aaa = pandas.read_excel('001.xls',sheet_name='Sheet1')

print("\n(2)第2行第3列的值:")
print(aaa.values[2,1])


报错变更了:
Traceback (most recent call last):
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 1, in <module>
    import pandas,xlwt,xlrd
  File "D:\Users\MSI\PycharmProjects\pythonProject\xlrd.py", line 5, in <module>
    aaa = pandas.read_excel('001.xls',sheet_name='Sheet1')
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1363, in __init__
    if import_optional_dependency("xlrd", errors="ignore") is None:
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 142, in import_optional_dependency
    version = get_version(module_to_get)
  File "C:\Users\MSI\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\compat\_optional.py", line 68, in get_version
    raise ImportError(f"Can't determine version for {module.__name__}")
ImportError: Can't determine version for xlrd
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 16:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表