Files
data-management/exchange/extract/exchange_extract_function.py
T
Yam ZhengLim 45dff350f5 Remove |
2022-10-01 12:34:19 +08:00

56 lines
1.8 KiB
Python

import csv
from operator import concat
import mysql.connector
import os
def connect_mysql(config):
try:
mydb = mysql.connector.connect(
host = config["host"],
user = config["user"],
password = config["password"],
database = config["database"])
return mydb
except Exception as e:
print(e)
print("[FUNCTION_ERROR]-connect_mysql")
return False
def mysql_dict_sql_to_csv(SQL_DICT, path, mysqldb, mysqlcursor):
temp_folder_path = path
# Check whether the specified path exists or not
temp_folder_path_isExist = os.path.exists(temp_folder_path)
if not temp_folder_path_isExist:
# Create a new directory because it does not exist
os.makedirs(temp_folder_path)
print("[Create Folder] exchange_temp_extract_csv")
for tablename,sql in SQL_DICT.items():
try:
mysqlcursor.execute(sql)
mysqlresult = mysqlcursor.fetchall()
mysqldbname = mysqldb.database
filename = '/{}_{}.csv'.format(mysqldbname, tablename)
headers = [col[0] for col in mysqlcursor.description] # get headers
mysqlresult.insert(0, tuple(headers))
with open(concat(path,filename), 'r') as infile, open(concat(path,filename), 'w') as outfile: #REMOVE ALL |
temp = infile.read().replace("|", "/")
outfile.write(temp)
fp = open(concat(path,filename), 'w', newline = '', encoding = 'utf-8-sig')
myFile = csv.writer(fp,delimiter='|',quotechar="'")
myFile.writerows(mysqlresult)
fp.close()
except Exception as e:
print(e)
print("[FUNCTION_ERROR]-write_to_csv:" , filename)
return False
finally:
print("[Download Complete]",filename)