import csv


def read_csv(filepath, dictionary=False, fieldnames=None, header=True, mode="r",
             encoding=None, errors=None, newline='', dialect='excel', **kwargs):
    """
    Read dataset from csv file.

    Open file with `open()` function and write with 
    `csv.reader` when dictionary is False or
    `csv.DictReader` when dictionary is True.

    Parameters
    ----------
    filepath: str
        filepath to save dataset.
    dictionary: bool, default False
        If DictReader should be used and 
        hence return iterable of dictionaries.
    fieldnames: iterable, default None
        An iterable of the keys in the dataset's dictionaries.
        If fieldnames is omitted, the values in the first row of
        csv file will be used as the fieldnames.
    header: bool, default True
        Should header or first row of the file be included in the dataset?
        Default is True, include header or first row.
    mode: str, default 'r'
        Mode in which file is opened.
        Default is `'r'`.
    encoding: str, optional
        The name of the encoding used to decode or encode the file.
        Default is whatever `locale.getpreferredencoding()` returns.
        `List of standand encoding
        <https://docs.python.org/3/library/codecs.html#standard-encodings>`_.
    errors: str, default None
        Specifies how encoding and decoding error should be handled
        The standard names include [1]_: `strict`, `ignore`, `replace`,
        `surrogateescape`, `xmlcharrefreplace`, `backslashreplace`,
        `namereplace`.
    newline: str, optional
        Default is `''` [1]_.
    dialect: string or subclass of `csv.Dialect
    <https://docs.python.org/3/library/csv.html#csv.Dialect>`_, optional
        Default is `'excel'`.
    **kwargs: other parameters for csv.reader or csv.Dict.Reader, optional
        For more details see [2]_, [3]_ and `Dialects and formatting parameters
        <https://docs.python.org/3/library/csv.html#csv-fmt-params>`

    Returns
    -------
    dataset: list of lists or dictionary
        The dataset read from the file.

    References
    ----------
    .. [1] `open() documentation
        <https://docs.python.org/3/library/functions.html#open>`_
    .. [2] `csv.reader documentation
        <https://docs.python.org/3/library/csv.html#csv.reader>`_
    .. [3] `csv.DictReader method documentation
        <https://docs.python.org/3/library/csv.html#csv.DictReader>`_
    """
    with open(filepath, mode=mode, encoding=encoding, errors=errors,
              newline=newline) as csvfile:
        if dictionary:
            reader = csv.DictReader(csvfile, fieldnames=fieldnames,
                dialect=dialect, **kwargs)
            return list(reader)
        else:
            reader = csv.reader(csvfile, dialect=dialect, **kwargs)
            if header:
                return list(reader)
            else:
                return list(reader)[1:]