在Pandas中导入excel文件给出错误

嗨,我很抱歉的麻烦,但我有一些主要问题导入一个Excel文件我希望有人可以给一些build议,我已经尝试了以前张贴在堆栈上的各种方法,他们似乎都没有工作。

import pandas as pd # making an excel data sheet df = pd.DataFrame({'Dox Dossage': [1,5,10,100,500,1000], 'MP': [7,35,70,700,3500,7000]}) writer = pd.ExcelWriter('Michael4-3-17', engine='xlsxwriter') df.to_excel(writer, sheet_name='Sheet1') writer.save() 

抓取excel文件并在excel中打开

 import glob print(glob.glob('Michael4-3-17*')) graphfile = glob.glob('Michael4-3-17*') df1 = pd.read_excel(open(graphfile), sheetname=None) 

我一直得到的错误是:

– > 4 df1 = pd.read_excel(open(graphfile),sheetname = None)

TypeError:预期的str,字节或os.PathLike对象,不是列表

它看起来像我有两个问题:(1)使用glob,你得到一个列表,和(2)使用open(graphfile) ,你没有传递一个文件名。 你可以简化很多只是做:

 graphfile = 'Michael4-3-17.xlsx' df1 = pd.read_excel(graphfile, sheetname=None) 

但也许你正在使用glob,因为你有多个文件,在这种情况下,你可以做到这一点(我有两个文件,Micahael4-3-17有和没有xlsx扩展名):

 import glob print(glob.glob('Michael4-3-17*')) graphfile = glob.glob('Michael4-3-17*') for file in graphfile: df1 = pd.read_excel(file, sheetname=None) print(df1) 

这产生:

 ['Michael4-3-17', 'Michael4-3-17.xlsx'] {'Sheet1': Dox Dossage MP 0 1 7 1 5 35 2 10 70 3 100 700 4 500 3500 5 1000 7000} {'Sheet1': Dox Dossage MP 0 1 7 1 5 35 2 10 70 3 100 700 4 500 3500 5 1000 7000}