Home > Software engineering >  pandas dataframe to excel
pandas dataframe to excel

Time:03-14

I am trying to save to an excel file from a panda dataframe. After some methods of scraping the data I end up having the final method, where I generate the data to an excel file. The problem is that I want the sheet_name to be an input variable for each scrape I do. But with the code below, I got the error:

ValueError: No engine for filetype: ''

def datacollection(self,filename):

    tbl= self.find_element_by_xpath("/html/body/form/div[3]/div[2]/div[3]/div[3]/div[1]/table").get_attribute('outerHTML')
    df=pd.read_html(tbl)
    print(df[0])
    print(type(df[0]))

    final=pd.DataFrame(df[0])

    final.to_excel(r'C:\Users\ADMIN\Desktop\PROJECTS\Python',sheet_name=f'{filename}')

CodePudding user response:

I believe the problem here is that you are asking it to write to a file called Python, without any file extension.

You could name it Python.xlsx for example.

Or, if Python was the directory name, then it should be Python/somefilename.xlsx

CodePudding user response:

You need to give a file extension for the excel file:

    final.to_excel(r'C:\Users\ADMIN\Desktop\PROJECTS\Python.xlsx',sheet_name=f'{filename}')
  • Related