I've been assigned a task which involves merging the pd.read_csv()
and the pd.read_excel()
functions together into one function called ingest()
. I've been trying to use regular expressions so that if the file contains a ".csv" it will execute the read_csv()
function or else it will read it as an excel file.
This is my code so far
rexf = re.compile((r'.csv'))
mo = rexf.search(dataframe)
if mo == True:
df = pd.read_csv(dataframe)
else:
df = pd.read_excel(dataframe)
return df
I then call this function with a file called "Smoking.csv". This file works when I use the pd.read_csv()
command but here it goes berserk and gives me this error
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found
Does anyone know why this may be, and how I can get the function to behave as intended? Thanks.