-
Notifications
You must be signed in to change notification settings - Fork 0
/
cns_cpf_update.py
98 lines (74 loc) · 3 KB
/
cns_cpf_update.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
89
90
91
92
93
94
95
96
97
98
import pandas as pd
from tqdm import tqdm
import psycopg2
from psycopg2 import Error
def main(comp):
competencia = comp
df = pd.read_csv(f'data/{competencia}.csv')
df = df[['CNS','NOME']].drop_duplicates().astype(str)
df.rename(columns = {'CNS':'cns','NOME':'nome'}, inplace = True)
#h = host, db = database, u = user, p = port, pw = password
def Connect(h, db, u, p, pw):
global df_prof
print("Conectando ao Banco de Dados")
try:
conn = psycopg2.connect(
host = h,
database = db,
user = u,
port = p,
password = pw
)
cur = conn.cursor()
cur.execute("SELECT version();")
rec = cur.fetchone()
print("You are connected to - ", rec, "\n")
sql = 'SELECT * FROM cns_prof_cpf'
df_prof = pd.read_sql(sql, conn)
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error)
finally:
if (conn):
cur.close()
conn.close()
print("PostgreSQL connection is closed")
return df_prof
df_prof = Connect('','','','','')
df_prof = df_prof[['cns','cpf']]
dados_novos = pd.merge(df, df_prof, how= 'left', on= 'cns')
dados_novos = dados_novos.loc[(dados_novos.cpf.isnull())]
dados_novos = dados_novos.drop_duplicates()
def Connect2(h, db, u, p, pw):
global dados_novos
print("Conectando ao Banco de Dados")
try:
conn = psycopg2.connect(
host = h,
database = db,
user = u,
port = p,
password = pw
)
cur = conn.cursor()
cur.execute("SELECT version();")
rec = cur.fetchone()
print("You are connected to - ", rec, "\n")
print("Inserting the Data en PostgreSQL table")
for i in tqdm(dados_novos.index):
sql = ''' INSERT INTO cns_prof_cpf
(CNS, NOME, CPF)
values ('%s','%s','%s') ''' % (dados_novos["cns"][i],
dados_novos["nome"][i],dados_novos["cpf"][i])
cur.execute(sql)
conn.commit()
print("Table was succesfullt updated")
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error)
finally:
if (conn):
cur.close()
conn.close()
print("PostgreSQL connection is closed")
Connect2('','','','','')
if __name__ == '__main__':
main()