-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_insert_elections.py
88 lines (68 loc) · 1.99 KB
/
create_insert_elections.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 re
import pandas as pd
INSERT_ELEICAO = """
INSERT INTO ELEICAO (turno, idResultado, ano, cpf)
VALUES ('{}', {}, '{}', '{}');
"""
SELECT_RESULTADO = """
SELECT idResultado FROM RESULTADO WHERE descricaoResultado = '{}'
"""
#SELECT_CANDIDATURA_CPF = """
#SELECT cpf FROM CANDIDATURA WHERE cpf = '{}' AND ano = '{}'
#"""
#
#SELECT_CANDIDATURA_ANO = """
#SELECT ano FROM CANDIDATURA WHERE cpf = '{}' AND ano = '{}'
#"""
def create_insert_string(row):
cpf = row[1]
ano = row[2]
idResultado = f"( {SELECT_RESULTADO.format(row[3])} )"
turno = row[4]
insert_sql = INSERT_ELEICAO.format(turno, idResultado, ano, cpf)
return insert_sql
def add_lines_to_file(lines):
f = open('./sql_scripts/popula_eleicoes.sql', 'w+')
f.write('USE eleicoes;\n')
for line in lines:
f.write(line)
f.close()
def read_cpfs():
regex = re.compile(r"(?P<cpf>\d+)(\t)(?P<year>\d{4})")
f = open('candidaturas.txt', 'r')
f = f.read().split('\n')
cpf_year_list = []
for x in f:
match = regex.search(x)
if match:
cpf_year_list.append((match.group('cpf'), match.group('year')))
return cpf_year_list
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, usecols=['cpf', 'ano', 'descricaoResultado', 'turno'])
counter = 0
lines = []
for row in data.itertuples():
if binary_search(cpfs, (str(row[1]), str(row[2]))):
line = create_insert_string(row)
lines.append(line)
counter += 1
print('tamanho = ', counter)
add_lines_to_file(lines)