May-07-2019, 02:48 AM
(May-06-2019, 08:14 PM)Yoriz Wrote: Maybe something like this will work
Note All code not tested
with open(source) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) insert_sql = """ INSERT INTO billing_info_test (InvoiceId, PayerAccountId, LinkedAccountId) VALUES (%s, %s, %s) """ rows = [] row_count = 0 for row in csv_reader: row_count += 1 rows.append(row) if row_count == 1000: cursor.executemany(insert_sql,rows) print(cursor.rowcount, 'inserted with LinkedAccountId', row[2], 'at', datetime.now().isoformat()) rows = [] row_count = 0 if rows: cursor.executemany(insert_sql,rows) print(cursor.rowcount, 'inserted with LinkedAccountId', row[2], 'at', datetime.now().isoformat()) print("Committing the DB") mydb.commit( cursor.close() mydb.close()
Using itertools
from itertools import zip_longest def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue) with open(source) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) insert_sql = """ INSERT INTO billing_info_test (InvoiceId, PayerAccountId, LinkedAccountId) VALUES (%s, %s, %s) """ for rows in grouper(csv_reader, 1000): cursor.executemany(insert_sql,rows) print(cursor.rowcount, 'inserted with LinkedAccountId', row[2], 'at', datetime.now().isoformat()) print("Committing the DB") mydb.commit( cursor.close() mydb.close()
Your first example worked really nicely! Thank you for that! It supercharged this process. I also want to learn the iterools method. I will try that tomorrow. Best wishes!