def superReadFile(filepath,**kwargs):
"""
Uses pandas.read_excel (on excel files) and returns a dataframe of the first sheet (unless sheet is specified in kwargs)
Uses superReadText (on .txt,.tsv, or .csv files) and returns a dataframe of the data.
One function to read almost all types of data files.
"""
if isinstance(filepath, pd.DataFrame):
return filepath
ext = os.path.splitext(filepath)[1].lower()
if ext in ['.xlsx', '.xls']:
kwargs.pop('dtype', None)
return pd.read_excel(filepath,**kwargs)
elif ext in ['.txt','.tsv','.csv']:
return superReadText(filepath, **kwargs)
elif ext in ['.gz', '.bz2', '.zip', 'xz']:
return superReadCSV(filepath, **kwargs)
elif ext in ['.h5']:
return pd.read_hdf(filepath)
else:
raise NotImplementedError("Unable to read '{}' files".format(ext))
评论列表
文章目录