Export de données en csv

Petit script en python pour extraire des données en csv d'une table ou plusieurs en utilisant le multiprocessing de python.

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cx_Oracle as cx
import pandas as pd
import os
import sys
import time
import csv
from multiprocessing import Process

tables = {'TABLE1', 'TABLE2', 'TABLE3', 'TABLE4'}

def extract(table,name):
        start = time.time()
        print('start '+table)
        os.environ["NLS_LANG"] = "FRENCH_FRANCE.AL32UTF8"
        con = cx.connect('user', 'password', 'localhost:1521/test', encoding = "UTF-8", nencoding = "UTF-8")
        try:
                os.remove('/u01/app/oracle/file/result/'+table+"_"+name+".csv")
        except:
                pass

        cursor = con.cursor()
        cursor.arraysize = 5000

        first=True
        df_ora = pd.read_sql('SELECT * FROM USER.'+table, con=con, chunksize=100000)
        for chunk in df_ora:
                if first:
                        chunk.to_csv('/u01/app/oracle/file/result/'+table+'_'+name+'.csv',index=False,mode="a",quoting=csv.QUOTE_NONNUMERIC)
                        first=False
                else:
                        chunk.to_csv('/u01/app/oracle/file/result/'+table+'_'+name+'.csv',index=False,mode="a",quoting=csv.QUOTE_NONNUMERIC,header=False)
        temps = time.time() - start
        print('stop '+table+' '+str(temps/60))

processes = []

for table in tables:
        p = Process(target=extract, args=(table,sys.argv[1]))
        p.start()
        processes.append(p)


for p in processes:
   p.join()