from io import StringIO
import csv


def data_to_string(data):
    """Write a list of dicts as a CSV string

    Args:
        data (list): A list of dicts, one per CSV row

    Returns:
        str: The stringified csv data, with a header row
    """
    with StringIO() as fout:
        fieldnames = data[0].keys()
        writer = csv.DictWriter(fout, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(data)
        return fout.getvalue()