import csv from itertools import islice def print_sql bulk print INSER

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import csv
from itertools import islice
def print_sql(bulk):
print (
"INSERT INTO new_loyalty_customer_list(country_code, customer_id)\nVALUES\n " +
",\n ".join(
"('%s', %s)" % (row[2], row[1])
for row in bulk
if row[2] and row[1]
) +
"\nON CONFLICT DO NOTHING;"
)
def ibulk(iter, bulk_size):
b = []
for i in iter:
b.append(i)
if len(b) >= bulk_size:
yield b
b = []
if b:
yield b
with open('pers_pols_initial_launch.csv', 'r+') as f:
reader = csv.reader(f)
sum = 0
for bulk in ibulk(reader, 10000):
print_sql(bulk)
sum += len(bulk)