Home > Software engineering >  Pandas .to_csv() gives "No engine for filetype: 'csv'"
Pandas .to_csv() gives "No engine for filetype: 'csv'"

Time:11-12

Basically, the title. The pd.Dataframe.to_csv() method works fine on my machine outside of the app code, but if I try to run it in a pyqt5 gui, I get the following error: No engine for filetype: 'csv'. Below is the function that causes the error. Note: if the user gives an .xlsx file, the .to_excel() function works fine. The file name the function uses is passed through a QFileDialog object. Here is the function causing the problem.

 def writeToFile(self, file, rows):
        
        try:
        
            new_df = pd.DataFrame(rows.items(), columns = ['Company Name', 'File Number'] )
        
            if self.stack.currentIndex() == 0:
            
                if file[-4:] == '.csv':
                
                    df = pd.read_csv(file)
                    new_df = pd.concat([df, new_df])
                
                else:
                
                    df = pd.read_excel(file)
                    new_df = pd.concat([df, new_df])
        
            if file[-4] == '.csv':
            
                new_df.to_csv(file, index = False, compression = None)
            
            else:
            
                new_df.to_excel(file)
            
            if self.flag_uids == []:
                
                return "All emails were written to a file"
            
            else:
                s = "All emails possible were written to a file. There were "   str(len(self.flag_uids))   " messages unable to be interpreted"
                return s
                                    
        except Exception as e:
            
            return e

CodePudding user response:

maybe the code is confusing the extensions, try to separate things.

def writeToFile(self, file, rows):
        
        try:
        
            new_df = pd.DataFrame(rows.items(), columns = ['Company Name', 'File Number'] )
            
        
            if self.stack.currentIndex() == 0:
            
                if file[-4:] == '.csv':
                
                    df_csv = pd.read_csv(file)
                    new_df_csv = pd.concat([df_csv, new_df])
                
                else:
                
                    df_excel = pd.read_excel(file)
                    new_df_excel = pd.concat([df_excel, new_df])
        
            if file[-4] == '.csv':
            
                new_df_csv.to_csv(file, index = False, compression = None)
            
            else:
            
                new_df_excel.to_excel(file)
            
            if self.flag_uids == []:
                
                return "All emails were written to a file"
            
            else:
                s = "All emails possible were written to a file. There were "   str(len(self.flag_uids))   " messages unable to be interpreted"
                return s
                                    
        except Exception as e:
            
            return e
  • Related