import csv
import io


def csvrow_to_yaml(fn_csv, isstringio=True):
    """
    Convert a comma delimited row of data into a
    a yaml representation that can be inserted into the yaml mix file.

    This procedure does not not know or care what each row means in the sense
    that it's just taking a comma or semicolon delimited row and converts it to YAML.

    :param fn_csv: csv filename
    :param isstringio: True to return StringIO object, False to return string
    :return: yaml version of csv row of data either as a StringIO object or a string.

    Example:
            Input:     0, 1, 0, 0
            Output:   [0, 1, 0, 0]
    """

    f_csv_in = open(fn_csv, 'r')
    dialect = csv.Sniffer().sniff(f_csv_in.read(1024), delimiters=',;')
    f_csv_in.seek(0)
    ash_data = csv.reader(f_csv_in, dialect)

    ash_list = [map(float, row) for row in ash_data]
    f_csv_in.close()

    yaml_str = ''
    for row in ash_list:
        yaml_str += (' - ' + str(row) + '\n')

    if isstringio:
        yaml_out = io.StringIO()
        yaml_out.write(yaml_str)
        return yaml_out.getvalue()
    else:
        return yaml_str