def export_csv_tuples(filename, tuples, header=None, dialect=None): if not dialect: dialect = get_dialect(filename) with io.open(filename, 'wt', newline='', encoding=dialect.encoding) as f: with csv.writer(f, dialect=dialect) as csv_out: if header: csv_out.writerow(header) for t in tuples: csv_out.writerow(t)
def export_csv_tuples_iter(filename, header=None, dialect=None, append=False): if not dialect: dialect = get_dialect(filename) closefd = filename != '-' if filename == '-': filename = sys.stdout.fileno() rows = yield with io.open(filename, 'at' if append else 'wt', newline='', encoding=dialect.encoding, closefd=closefd) as f: with csv.writer(f, dialect=dialect) as csv_out: while True: csv_out.writerows(rows) rows = yield