import csv


def read_csv_values(file_name, convert=False, delimiter=',', quotechar='|'):
    """Read any csv file with headers.

    You can extract the multiple lists from the headers in the CSV file.
    In your own script, call this function and assign to data variable,
    then extract the lists to the variables. Example:
    data = fh.read_csv_values(mycsv.csv)
    sg_name_list = data['sgname']
    policy_list = data['policy']

    :param file_name: path to CSV file -- str
    :param convert: convert strings to equivalent data type -- bool
    :param delimiter: delimiter kwarg for csv DictReader object -- str
    :param quotechar: quotechar kwarg for csv DictReader object -- str
    :returns: CSV parsed data -- dict
    """

    def _convert(s):
        # Convert a string representation of a bool or number to equivalent
        # types that can be used in Python
        if convert:
            try:
                if s.lower() == 'true':
                    s = True
                elif s.lower() == 'false':
                    s = False
                else:
                    s = float(s)
            except ValueError:
                pass
        return s

    # open the file in universal line ending mode
    with open(file_name, newline='') as infile:
        # read the file as a dictionary for each row ({header : value})
        reader = csv.DictReader(
            infile, delimiter=delimiter, quotechar=quotechar)
        data = dict()
        for row in reader:
            for header, value in row.items():
                try:
                    data[header].append(_convert(value))
                except KeyError:
                    data[header] = [_convert(value)]
    return data