import csv ORCHARD_VENDOR_ID = 0 ORCHARD_VENDOR_NM = 1 ORCHARD_SUBACCT_ID = 2 ORCHARD_SUBACCT_NM = 3 FIN_LABEL_CD = 4 FIN_LABEL_NM = 5 REP_OWNER_CD = 6 REP_OWNER_NM = 7 GRAS_IMPRINT_LABEL_KEY = 8 GRAS_IMPRINT_LABEL_NM = 9 lines = csv.reader(open('sony_sample.csv', 'r'), delimiter=',', quotechar='"') def format_value(name): quote = '\'' if '\'' in name: quote = '"' return '{start}{name}{end}'.format(start=quote, name=name, end=quote) statement = '' for count, line in enumerate(lines, 0): if count % 200 == 0: statement += 'INSERT INTO field_manager (' \ 'vendor_id, subaccount_id, financial_label_code, financial_label_name, ' \ 'rep_owner_code, rep_owner_name, gras_imprint_code, gras_imprint_name) VALUES \n' statement += '({vendor_id}, {subaccount_id}, {financial_label_code}, ' \ '{financial_label_name}, {rep_owner_code}, {rep_owner_name}, ' \ '{gras_imprint_code}, {gras_imprint_name}),\n'.format( \ vendor_id = line[ORCHARD_VENDOR_ID], \ subaccount_id = line[ORCHARD_SUBACCT_ID], \ financial_label_code = format_value(line[FIN_LABEL_CD]), \ financial_label_name = format_value(line[FIN_LABEL_NM]), \ rep_owner_code = format_value(line[REP_OWNER_CD]), \ rep_owner_name = format_value(line[REP_OWNER_NM]), \ gras_imprint_code = format_value(line[GRAS_IMPRINT_LABEL_KEY]), \ gras_imprint_name = format_value(line[GRAS_IMPRINT_LABEL_NM]) ) if (count + 1) % 200 == 0: statement = statement.strip(',\n') + ';\n' print (statement) statement = '' if statement: statement = statement.strip(',\n') + ';\n' print(statement) statement = ''