from io import StringIO
import csv


def encode_csv(dataset):
    """Takes a list of rows and returns it as a CSV string.

    Parameters:
        dataset (list[row]): A list of rows. Each row is a list of primitive
            values.

    Returns:
        str: A CSV representation of the dataset.

    >>> encode_csv([[1, 2, 3], [4, 5, 6]])
    '1,2,3\\r\\n4,5,6\\r\\n'

    >>> encode_csv([['a, b, c', 'xyz', None, 1.0]])
    '"a, b, c",xyz,,1.0\\r\\n'

    >>> encode_csv([['he said, "well, ok" and then left', 'foo', 'bar']])
    '"he said, ""well, ok"" and then left",foo,bar\\r\\n'
    """

    result = StringIO()
    writer = csv.writer(result, quoting=csv.QUOTE_MINIMAL)
    for row in dataset:
        writer.writerow(row)
    return result.getvalue()