【发布时间】:2016-11-10 10:34:07
【问题描述】:
您好,我正在尝试通过几个 excel 文件运行我的 python 代码,并从每个文件中获取数据并保存到数据框中。这是我的代码..
import os
import glob
import pandas as pd
path =r'C:\Users\user1\Desktop\test'
files = os.listdir(path)
files_xls = [f for f in files if f[-3:] == 'xls']
df = pd.DataFrame()
for f in files_xls:
filename, ext = os.path.splitext(f)
data = pd.read_excel(f, filename)
df = df.append(data)
a = df.describe()
print (a)
我收到此错误.. 我正在处理的文件夹中的第一个文件是 test.xls
Traceback (most recent call last):
File "test.py", line 20, in <module>
data = pd.read_excel(f, filename)
File "C:\Users\user1\AppData\Local\Programs\Python\Python35-32\lib\site- packages\pandas\io\excel.py", line 170, in read_excel
io = ExcelFile(io, engine=engine)
File "C:\Users\user1\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\io\excel.py", line 227, in __init__
self.book = xlrd.open_workbook(io)
File "C:\Users\user1\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlrd\__init__.py", line 395, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test.xls'
【问题讨论】:
-
我检查了您的代码,将
data = pd.read_excel(f, filename)更改为data = pd.read_excel(f)并正常工作。为什么使用filename参数? -
我试过了..它也给了我同样的错误
-
你试过下面的解决方案了吗?
标签: python excel python-3.x pandas dataframe