diff --git a/adega_crypt.py b/adega_crypt.py index 9e688d0ecee1bdb02f41c97aa942f33b1f7989ba..0944da4e3086a5c6b7a97b5ecf9198146b0e67c8 100755 --- a/adega_crypt.py +++ b/adega_crypt.py @@ -6,6 +6,8 @@ import sys import pandas as pd import numpy as np import os +import shutil +import re @@ -35,8 +37,40 @@ $ %s -d relatorio.csv def crypt(name): + + notify_empty_lines = False + notify_comma_decimal_separator = False + + if not os.path.exists('.'+name): + shutil.copyfile(name, '.'+name) + else: + print('arquivo .%s ja existe, abortando' % name) + sys.exit(1) + + comma_separator = re.compile(ur',33333333|,66666666') + + orig = open(name, 'r') + rep = open('.'+name, 'w+') + + for line in orig: + if len(line) == 0: + notify_empty_lines = True + elif comma_separator.search(line): + notify_comma_decimal_separator = True + + #l = line.decode('utf8').replace(r',33333333', r'.33333333').replace(r',66666666', r'.66666666').encode('utf8') + l = line.replace(r',33333333', r'.33333333').replace(r',66666666', r'.66666666') + + rep.write(l) + else: + rep.write(line) + + orig.close() + rep.close() + + #sys.exit(5) - data = pd.read_csv(name) + data = pd.read_csv('.'+name) if os.path.exists(TABLE_FILE): table = pd.read_csv(TABLE_FILE) @@ -46,7 +80,7 @@ def crypt(name): if not os.path.exists(dirname): os.makedirs(dirname) - table = pd.DataFrame(columns=["ID", "MATR_ALUNO", "NOME_ALUNO"]) + table = pd.DataFrame(columns=["ID", "MATR_ALUNO", "NOME_PESSOA"]) table.to_csv(TABLE_FILE, index=False) @@ -63,15 +97,18 @@ def crypt(name): # if "NOME_ALUNO" in data.columns: + data.rename(columns={'NOME_ALUNO': 'NOME_PESSOA'}, inplace=True) + + if "NOME_PESSOA" in data.columns: - table_append = pd.merge(table_append, data[["MATR_ALUNO", "NOME_ALUNO"]], how="left", on="MATR_ALUNO") + table_append = pd.merge(table_append, data[["MATR_ALUNO", "NOME_PESSOA"]], how="left", on="MATR_ALUNO") UTF8toupper = lambda s: s.decode("utf8").upper().encode("utf8") - table_append["NOME_ALUNO"] = table_append.NOME_ALUNO.apply(UTF8toupper) + table_append["NOME_PESSOA"] = table_append.NOME_PESSOA.apply(UTF8toupper) else: - table_append["NOME_ALUNO"] = "" + table_append["NOME_PESSOA"] = "" # pega todos os registros que ainda não existem no table @@ -106,8 +143,8 @@ def crypt(name): del data["MATR_ALUNO"] - if "NOME_ALUNO" in data.columns: - del data["NOME_ALUNO"] + if "NOME_PESSOA" in data.columns: + del data["NOME_PESSOA"] data.rename(columns={"ID": "MATR_ALUNO"}, inplace=True) @@ -129,6 +166,17 @@ def crypt(name): # escreve o arquivo # data.to_csv(".".join(split_name), index=False) + + os.remove('.'+name) + + if notify_empty_lines or notify_comma_decimal_separator: + print "Warnings:" + + if notify_empty_lines: + print "Linha em branco presente no arquivo" + + if notify_comma_decimal_separator: + print "virgula foi usada como separador de campo decimal" return data diff --git a/check.py b/check.py old mode 100644 new mode 100755 index 21ecc5fb47b346f88bcb182cc8f6f4230086bcbf..4b60ae33cd4be60f97fe7db5113ebbedd8a3459c --- a/check.py +++ b/check.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# coding: utf-8 + import pandas as pd import os