pyhton调用pandas来读取excel文件并输出,结果报错
本帖最后由 王嘉伟 于 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, 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(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. Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
自己百度,报错信息都给你了 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模块,我之前就安装了这个模块了呀 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模块吗 import pandas as pd
aaa = pd.read_excel('001.xls',sheet_name='Sheet1')
##print(df.values)
print("\n(2)第2行第3列的值:")
print(aaa.iloc)
试试这个 王嘉伟 发表于 2022-3-10 19:15
我百度搜索了,网上让我安装xlrd模块,我之前就安装了这个模块了呀
pandas 是依赖xlrd的, 你可以删除以前的xlrd版本.重新安装pandas会自动安装覆盖的 hh920081949 发表于 2022-3-10 19:35
import pandas as pd
依旧报错,显示:
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, 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(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. sxfxtf 发表于 2022-3-10 19:37
pandas 是依赖xlrd的, 你可以删除以前的xlrd版本.重新安装pandas会自动安装覆盖的
我删除了xlrd模块,重新安装的pandas模块也是依旧报错 王嘉伟 发表于 2022-3-10 19:46
我删除了xlrd模块,重新安装的pandas模块也是依旧报错
那i再单独安装xlrd 安装版本要大于 1.0.0 hh920081949 发表于 2022-3-10 19:35
import pandas as pd
我改了一下代码,代码变更如下:
import pandas,xlwt,xlrd
aaa = pandas.read_excel('001.xls',sheet_name='Sheet1')
print("\n(2)第2行第3列的值:")
print(aaa.values)
报错变更了:
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