import csv import typing from io import StringIO def dump_data_to_csv( data: typing.Iterable, headers: typing.Optional[typing.Iterable] = None ) -> str: """ Dumps any iterable of rows to csv string. Intended to use with queries or any other iterables of iterables """ content = StringIO() writer = csv.writer(content) if headers: writer.writerow(headers) for row in data: writer.writerow(row) return content.getvalue()