Python 使用ExcelFile()时,我得到的值错误:无效的文件路径或缓冲区对象类型:<;类别';pandas.core.frame.DataFrame'&燃气轮机;
这是我试过的代码。我正在尝试读取excel文件中的工作表,因此尝试了ExcelFile(),但不确定为什么会出现以下错误Python 使用ExcelFile()时,我得到的值错误:无效的文件路径或缓冲区对象类型:<;类别';pandas.core.frame.DataFrame'&燃气轮机;,python,python-3.x,pandas,jupyter-notebook,Python,Python 3.x,Pandas,Jupyter Notebook,这是我试过的代码。我正在尝试读取excel文件中的工作表,因此尝试了ExcelFile(),但不确定为什么会出现以下错误 # Import pandas import pandas as pd # Assign spreadsheet filename: file xls = pd.read_excel('battledeath.xlsx') # Load spreadsheet: xls xls = pd.ExcelFile(pd.read_excel('battledeath.xls'))
# Import pandas
import pandas as pd
# Assign spreadsheet filename: file
xls = pd.read_excel('battledeath.xlsx')
# Load spreadsheet: xls
xls = pd.ExcelFile(pd.read_excel('battledeath.xls'))
我看到了这个错误:
ValueError Traceback (most recent call last)
<ipython-input-1-c639baabe58f> in <module>
7
8 # Load spreadsheet: xls
----> 9 xls = pd.ExcelFile(pd.read_excel('battledeath.xls'))
10
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel.py in __init__(self, io, **kwds)
374 io = _urlopen(self._io)
375 elif not isinstance(self.io, (ExcelFile, xlrd.Book)):
--> 376 io, _, _, _ = get_filepath_or_buffer(self._io)
377
378 if engine == 'xlrd' and isinstance(io, xlrd.Book):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode)
216 if not is_file_like(filepath_or_buffer):
217 msg = "Invalid file path or buffer object type: {_type}"
--> 218 raise ValueError(msg.format(_type=type(filepath_or_buffer)))
219
220 return filepath_or_buffer, None, compression, False
ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>
ValueError回溯(最近一次调用)
在里面
7.
8#加载电子表格:xls
---->9 xls=pd.ExcelFile(pd.read_excel('battledeath.xls'))
10
C:\ProgramData\Anaconda3\lib\site packages\pandas\io\excel.py in\uuuuu init\uuuuuu(self,io,**kwds)
374 io=_urlopen(self._io)
375如果不存在(self.io,(ExcelFile,xlrd.Book)):
-->376 io,u,u,u=获取文件路径或缓冲区(self.\u io)
377
378如果引擎=='xlrd'和isinstance(io,xlrd.Book):
C:\ProgramData\Anaconda3\lib\site packages\pandas\io\common.py在get\u filepath\u或\u buffer中(filepath\u或\u buffer,编码,压缩,模式)
216如果不是类似文件(文件路径或缓冲区):
217 msg=“无效的文件路径或缓冲区对象类型:{u type}”
-->218 raise VALUERROR(消息格式(_type=type(文件路径或缓冲区)))
219
220返回文件路径\或\缓冲区,无,压缩,错误
ValueError:无效的文件路径或缓冲区对象类型:
我想你可能会被read\u excel
和ExcelFile
之间的差异搞混了,总之,一个是函数,如果需要的话,它会顺便为你创建一个ExcelFile
实例,另一个是类,但是,老实说,你不必太担心这个
总之,您得到的错误正是tin上显示的错误,ExcelFile
不明白为什么要将数据帧传递给它的构造函数。要解决您的问题,只需使用
xls = pd.read_excel('battledeath.xlsx')
现在,
xls
是一个包含数据的数据框。还有,给你一个提示,请阅读谢谢Danyul。这对我有用。另外,我认为我可以简单地使用它,将熊猫作为pd文件='battledeath.xlsx'xls=pd.ExcelFile(文件)打印(xls.sheet_名称)导入