Michael
Michael

Reputation: 39

Importing excel file in Pandas Gives Error

Hi I am sorry for the bother but I am having some major issues importing an excel file I was hoping someone could give some advice, I already tried various methods that were previously posted on stackflow none of them seem to be working.

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()

grabbing excel file and opening in excel

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

Error that I keep getting is:

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

TypeError: expected str, bytes or os.PathLike object, not list

Upvotes: 1

Views: 900

Answers (1)

Kewl
Kewl

Reputation: 3417

It looks to me like there are two problems: (1) Using glob, you get back a list, and (2) using open(graphfile), you aren't passing a filename. You can simplify a lot just by doing:

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

But maybe you're using glob because you have multiple files, in which case you can do this (I have two files, Micahael4-3-17 with and without the xlsx extension):

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)

which produces:

['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}

Upvotes: 2

Related Questions