-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinserts.py
49 lines (40 loc) · 1.32 KB
/
inserts.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pandas as pd
import data_const as c
import os
"""
A través de este script se modelan los procesos de
transcripción de datasets bien formados
a inserts sql, escritos en archivos.sql
"""
def main(dataset, table_name):
"""
PARAMETERS
----------
dataset : str
Ruta del dataset
table_name :str
La tabla donde son requeridos los inserts
"""
filename = os.path.join(c.filepath_sql, f'{table_name}.sql')
file = open(filename, 'w')
data = pd.read_csv(dataset)
atr_names = str(tuple(data.columns.tolist())).replace("'", "")
for i in data.index:
attr = str(tuple(data.iloc[i].values))
attr = attr.replace('False', "'False'")
attr = attr.replace('True', "'True'")
attr = attr.replace('nan', "NULL")
sql_line = f'insert into {table_name} {atr_names} values {attr};'
file.write(sql_line + '\n')
file.close()
if __name__ == '__main__':
files = [c.filepath_client,
c.filepath_partner,
c.filepath_vehicle,
c.filepath_travel,
c.filepath_infraction,
c.filepath_board,
c.filepath_schedule]
names = ['cliente', 'socio', 'vehiculo', 'viaje', 'infraccion', 'abordar', 'programar']
for file, name in zip(files, names):
main(file, name)