Home > Software engineering >  Pandas file import if statement issue
Pandas file import if statement issue

Time:05-04

I'm having an issue with my if statement attempting to import my listing in a pandas DF.

The file_extension[1] variable takes the filename from the input file and attempts to input the database depending on the file extension. The file I'm testing has a csv extension and I can see this from the print statement I've added. However I keep on getting an error message where its attempting to import the csv file under the read excel if statement and obviously doesn't work given its a CSV file. Any thoughts on why this might be happening? It should be importing it as csv given the extension is csv...

FYI this is the error message I get - ValueError: Excel file format cannot be determined, you must specify an engine manually.

Code extract below:

        file_extension = filename.split(".", 1)

        print(file_extension[1])

        if file_extension[1].lower() == 'tsv':
            df = pd.read_csv(filename, sep='\t', dtype={'Account Code': 'string', 'Net': 'float'},
                             low_memory=False)  # import TSV

        elif file_extension[1].lower() == 'xlsx' or 'xls' or 'xlsm':
            df = pd.read_excel(filename, sheet_name=0, na_filter=False, dtype={'Account Code': 'string', 'Net': 'float'})
            print('Imported as excel')

        elif file_extension[1].lower() == 'csv':
            df = pd.read_csv(filename, sep=',', dtype={'Account Code': 'string', 'Net': 'float'}, low_memory=False)
        else:
            print('File type not supported')

Thanks for any help in advance !

CodePudding user response:

your condition in elif statement is wrong. you should update line

elif file_extension[1].lower() == 'xlsx' or 'xls' or 'xlsm':

with

elif file_extension[1].lower() in ('xlsx', 'xls' , 'xlsm'):

  • Related