import zipfile
import io
import gzip


def open_file(file):
    """Opens a CSV file.

    The file can either be in plain text (.csv), zipped (.csv.zip), or gzipped (.csv.gz)

    Parameters
    ----------
    file : str
        The file to open

    Returns
    -------
    file-object
        A file object
    """
    if file.endswith(".zip"):
        zip_file = zipfile.ZipFile(file, mode="r")
        file = zip_file.open(zip_file.infolist()[0], mode="r")
        return io.TextIOWrapper(file)
    elif file.endswith(".gz"):
        return gzip.open(file, mode="rt")
    else:
        return open(file, mode='r')