Skip to content

Instantly share code, notes, and snippets.

@OlivierBinette
Last active August 7, 2022 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlivierBinette/97300fcb1b30a825be13000eba10b9cd to your computer and use it in GitHub Desktop.
Save OlivierBinette/97300fcb1b30a825be13000eba10b9cd to your computer and use it in GitHub Desktop.
Read pandas dataframe according to file extension
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