Created
June 26, 2013 08:41
-
-
Save kmmbvnr/5865799 to your computer and use it in GitHub Desktop.
Excel to Oracle loading sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode:python; coding: utf-8 -*- | |
""" | |
Usage: | |
import_request <username> | |
""" | |
import cx_Oracle | |
import getpass | |
import openpyxl as px | |
from datetime import datetime, time | |
from docopt import docopt | |
columns = [ | |
"id", "id_version", "check_date", "req_date", "req_city", | |
"ander_name", "manager_name", "person_lastname", "person_firstname", "person_middlename", | |
"person_birthdate", "person_genger", "citezenship", "birthplace", "education", | |
"social_status", "auto_producer", "auto_model", "auto_year", "auto_volume", | |
"auto_kpp_type", "auto_amount", "first_amount", "first_amount_pct", "cred_amount", | |
"cred_srok", "cred_currency", "seller_name", "doc_seria", "doc_number", | |
"doc_date", "doc_issuer", "addr_reg_type", "addr_reg_date", "addr_reg_date_end", | |
"addr_reg_index", "addr_reg_region", "addr_reg_obl", "addr_reg_city", "addr_reg_street", | |
"addr_reg_house", "addr_reg_corp", "addr_reg_flat", "addr_fact_index", "addr_fact_region", | |
"addr_fact_obl", "addr_fact_city", "addr_fact_street", "addr_fact_house", "addr_fact_corp", | |
"addr_fact_flat", "addr_fact_doc", "phone_reg_addr", "phone_fact_addr", "phone_mobile", | |
"phone_add", "email", "phone_kin", "kin_lastname", "kin_status", | |
"kin_description", "kin_mobile", "kin_life_fio", "kin_life_status", "kin_life_description", | |
"person_mariage", "person_child_cnt", "work_org_name", "work_ur_addr", "work_fact_addr", | |
"work_status", "work_standing", "work_income", "work_chief_name", "work_phone", | |
"work_phone_mobile", "work_phone_description", "partner_income", "other_income", "rent_income", | |
"business_income", "other1_loan_bank", "other1_loan_amount", "other1_loan_debt", "other1_loan_startdate", | |
"other1_loan_enddate", "other1_loan_pay", "other2_loan_bank", "other2_loan_amount", "other2_loan_debt", | |
"other2_loan_startdate", "other2_loan_enddate", "other2_loan_pay", "other3_loan_bank", "other3_loan_amount", | |
"other3_loan_debt", "other3_loan_startdate", "other3_loan_enddate", "other3_loan_pay", "other4_loan_bank", | |
"other4_loan_amount", "other4_loan_debt", "other4_loan_startdate", "other4_loan_enddate", "other4_loan_pay", | |
"over_lim", "over_term", "appearance_1", "appearance_2", "appearance_3", | |
"appearance_4", "appearance_5", "appearance_6", "appearance_7", "appearance_8", | |
"appearance_9", "appearance_10", "request_date", "last_ander_name", "bank_agent_name", | |
"ander1_mark", "ander2_mark", "ander3_mark", "ander4_mark", "ander5_mark", | |
"ander6_mark", "ander7_mark", "ander8_mark", "ander9_mark", "ander10_mark", | |
"ander11_mark", "ander12_mark", "ander13_mark", "scorring1_mark", "scorring2_mark", | |
"scorring3_mark", "scorring4_mark", "scorring5_mark", "scorring6_mark", "scorring7_mark", | |
"scorring8_mark", "scorring9_mark", "scorring10_mark", "scorring11_mark", "scorring12_mark", | |
"scorring13_mark", "scorring14_mark", "scorring15_mark", "scorring16_mark", "scorring17_mark", | |
"scorring18_mark", "scorring19_mark", "ander1_add", "ander2_add", "ander3_add", | |
"ander4_add", "ander5_add", "ander6_add", "ander7_add", "ander8_add", | |
"ander9_add", "ander10_add", "ander11_add", "ander12_add", "ander13_add", | |
"ander14_add", "ander15_add", "ander16_add", "timing1_mark", "timing2_mark", | |
"timing3_mark", "timing4_mark", "timing5_mark", "timing6_mark", "result_cred_date", | |
"result_cred_acc", "result_cred_num", "result_cred_amount" | |
] | |
def fix_value(value): | |
if isinstance(value, time): | |
return "%s:%s" % (value.hour, value.minute) | |
elif isinstance(value, datetime): | |
return value.strftime("%d.%m.%Y") | |
elif isinstance(value, float): | |
return "%.2f" % value | |
elif value: | |
return value.replace('\xab', ' ').replace('\xbb', ' ').replace('\xc9', ' ').replace('\u2212', ' ') | |
return value | |
if __name__ == '__main__': | |
options = docopt(__doc__) | |
password = getpass.getpass() | |
connection = cx_Oracle.connect('%s/%s@bank' % (options['<username>'], password)) | |
workbook = px.load_workbook('requests.xlsx', use_iterators = True) | |
sheet = workbook.get_sheet_by_name('New_MAX') | |
ins_query = """ | |
insert into PMV.LOANS_REQUESTS(%s) | |
values (%s) | |
""" % (", ".join(columns), ", ".join(":%s"%column for column in columns)) | |
ins_cursor = connection.cursor() | |
ins_cursor.prepare(ins_query) | |
for num, row in enumerate(sheet.iter_rows()): | |
if num < 3: | |
continue | |
try: | |
values = [fix_value(cell.internal_value) for cell in row[0:178]] | |
data = dict(zip(columns, values)) | |
ins_cursor.execute(None, data) | |
if num % 10 == 0: | |
print(num) | |
if num % 100 == 0: | |
connection.commit() | |
except Exception as e: | |
print(num, values) | |
raise e | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment