File size: 711 Bytes
531d507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""File-loading utilities.  Supports CSV, TSV, and Excel."""

from pathlib import Path

import pandas as pd

SUPPORTED_EXTENSIONS = {".csv", ".tsv", ".xlsx", ".xls"}


def load_table(path: str) -> pd.DataFrame:
    """Load a table from *path*.  Format is detected from the file extension."""
    p = Path(path)
    if not p.exists():
        raise FileNotFoundError(f"File not found: {path}")

    ext = p.suffix.lower()
    if ext == ".csv":
        return pd.read_csv(p)
    if ext == ".tsv":
        return pd.read_csv(p, sep="\t")
    if ext in (".xlsx", ".xls"):
        return pd.read_excel(p)

    raise ValueError(
        f"Unsupported format '{ext}'.  Supported: {sorted(SUPPORTED_EXTENSIONS)}"
    )