-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_insert_candidate.py
88 lines (67 loc) · 1.8 KB
/
create_insert_candidate.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
85
86
87
88
import pandas as pd
import bisect
MAX_CANDIDATES = 1000
INSERT_CANDIDATE = """
INSERT INTO CANDIDATO (cpf, nomeUrna, sexo, nomeCandidato, dtNascimento)
VALUES ('{}', '{}', '{}', '{}', {});
"""
def create_insert_string(row):
dt = str(row[6])
dt = f"'{dt[-4:]}-{dt[2:4]}-{dt[0:2]}'"
if len(dt) != 12:
dt = "NULL"
nome_urna = str(row[3]).replace("'", "")
nome_candidato = str(row[5]).replace("'", "")
insert_sql = INSERT_CANDIDATE.format(
row[2],
nome_urna,
row[4],
nome_candidato,
dt
)
return insert_sql
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
def create_cpfs_file(cpfs):
f = open('cpfs.txt', 'w+')
for cpf in cpfs:
f.write(str(cpf)+'\n')
f.close()
def add_lines_to_file(lines):
f = open('./sql_scripts/popula_candidatos.sql', 'w+')
f.write('USE eleicoes;\n')
for line in lines:
f.write(line)
f.close()
if __name__ == '__main__':
filename = 'new_candidates.csv'
data = pd.read_csv(filename, parse_dates=['dtNascimento'])
counter = 0
lines = []
cpfs = []
for row in data.itertuples():
# if counter < MAX_CANDIDATES:
cpf = row[2]
if not binary_search(cpfs, cpf):
bisect.insort(cpfs, cpf)
counter += 1
line = create_insert_string(row)
lines.append(line)
# else:
# break
print('tamanho = ', counter)
create_cpfs_file(set(cpfs))
add_lines_to_file(lines)