def write_to_table_via_csv(table, rows, connection): with temp_csv(mode='r+', tmp_dir=TEMPORARY_DIR) as csv_file: csv.writer(csv_file, delimiter='\t').writerows(rows) csv_file.seek(0, 0) connection.connection.cursor().copy_from(csv_file, sep='\t', null='', table=table.name) connection.connection.commit()
def vcf_to_csv(vcfdata, columns, filename, **kwargs): """Insert all flattened records in VCF into a CSV. Args: vcfdata: A vcf.Reader of records. columns: A list of columns. filename: The name of the CSV to be written to. Returns: The name of the CSV file. """ tmp_dir = kwargs.get('temporary_dir', None) if filename is None: csvfile = temp_csv(mode='w', tmp_dir=tmp_dir) filename = csvfile.name else: csvfile = open(filename, 'w') relations = records_to_relations(vcfdata, columns, **kwargs) csv.writer(csvfile).writerows(relations) csvfile.close() return filename