-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_insert_candidatura.py
84 lines (64 loc) · 1.86 KB
/
create_insert_candidatura.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pandas as pd
INSERT_CANDIDATURA = """
INSERT INTO CANDIDATURA (cpf, ano, unidadeEleitoral, unidadeFederativa,
idCargo, idSituacao, sigla)
VALUES ('{}', '{}', '{}', '{}', {}, {}, '{}');
"""
SELECT_CARGO = """
SELECT idCargo FROM CARGO WHERE descricaoCargo = '{}'
"""
SELECT_SITUACAO = """
SELECT idSituacao FROM SITUACAO WHERE descricaoSituacao = '{}'
"""
def create_insert_string(row):
cpf = row[2]
ano = row[11]
unidadeEleitoral = row[12]
unidadeFederativa = row[13]
idCargo = f"( {SELECT_CARGO.format(row[7])} )"
idSituacao = f"( {SELECT_SITUACAO.format(row[8])} )"
sigla = row[9]
insert_sql = INSERT_CANDIDATURA.format(
cpf, ano, unidadeEleitoral, unidadeFederativa, idCargo, idSituacao,
sigla
)
return insert_sql
def add_lines_to_file(lines):
f = open('./sql_scripts/popula_candidatura.sql', 'w+')
f.write('USE eleicoes;\n')
for line in lines:
f.write(line)
f.close()
def read_cpfs():
f = open('cpfs.txt', 'r')
f = f.read().split('\n')
return f
def binary_search(array, target):
lower = 0
upper = len(array)
while lower < upper:
x = lower + (upper - lower) // 2
val = array[x]
if target == val:
return True
elif target > val:
if lower == x:
break
lower = x
elif target < val:
upper = x
return False
if __name__ == '__main__':
filename = 'new_candidates.csv'
cpfs = read_cpfs()
cpfs.sort()
data = pd.read_csv(filename, parse_dates=['dtNascimento'])
counter = 0
lines = []
for row in data.itertuples():
if binary_search(cpfs, str(row[2])):
line = create_insert_string(row)
lines.append(line)
counter += 1
print('tamanho = ', counter)
add_lines_to_file(lines)