from io import StringIO
import csv


def to_csv(val):
    """
    Modifies odML values for serialization to strings and files.

    :param val: odML value.
    :return: modified value string.
    """
    # Make sure all individual values do not contain
    # leading or trailing whitespaces.
    unicode_values = list(map(str.strip, map(str, val)))
    stream = StringIO()
    writer = csv.writer(stream, dialect="excel")
    writer.writerow(unicode_values)
    # Strip any csv.writer added carriage return line feeds
    # and double quotes before saving.
    csv_string = stream.getvalue().strip().strip('"')
    if len(unicode_values) > 1:
        csv_string = "[" + csv_string + "]"
    return csv_string