Last active
August 7, 2022 17:41
-
-
Save OlivierBinette/97300fcb1b30a825be13000eba10b9cd to your computer and use it in GitHub Desktop.
Read pandas dataframe according to file extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
def read_auto(datapath): | |
_, ext = os.path.splitext(datapath) | |
if ext == ".csv": | |
return pd.read_csv(datapath) | |
elif ext == ".tsv": | |
return pd.read_csv(datapath, sep="\t") | |
elif ext in [".parquet", ".pq", ".parq"]: | |
return pd.read_parquet(datapath) | |
elif ext in [".xlsx", ".xls"]: | |
return pd.read_excel(datapath) | |
else: | |
raise Exception("Unsupported file type. Should be one of csv, tsv, parquet, or xlsx.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment