ZIP indir
0
main
1 dal
git@carbon4code.com:ahmet/Python-for-Data-Science.git
Ahmet Kerem Piscine Final
10 commit
load_csv.py 952 B
import pandas as pd


def load(path: str) -> pd.DataFrame:
    """Load a CSV file, print its dimensions and return it.

    On any failure a clear error message is printed and None is returned.
    """
    try:
        if not isinstance(path, str):
            raise TypeError("path must be a string")
        dataset = pd.read_csv(path)
        print(f"Loading dataset of dimensions {dataset.shape}")
        return dataset
    except FileNotFoundError:
        print(f"Error: file '{path}' not found")
    except pd.errors.EmptyDataError:
        print(f"Error: file '{path}' is empty")
    except (pd.errors.ParserError, UnicodeDecodeError):
        print(f"Error: file '{path}' is not a valid CSV")
    except Exception as error:
        print(f"Error: {error}")
    return None


def main() -> None:
    """Load the life expectancy dataset and display it."""
    print(load("../life_expectancy_years.csv"))


if __name__ == "__main__":
    main()