forked from fabioserpa/CNPJ-full
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permite que os arquivo empresas.csv possa ser separado por UF ou cidade
- Loading branch information
1 parent
b3c78d6
commit caa3128
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FILES_LOCATION=/media/Arquivos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea* | ||
__pycache__/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
def get_header_index(column): | ||
header = { | ||
'cnpj': 0, | ||
'matriz_filial': 1, | ||
'razao_social': 2, | ||
'nome_fantasia' : 3, | ||
'situacao' : 4, | ||
'data_situacao' : 5, | ||
'motivo_situacao' : 6, | ||
'nm_cidade_exterior' : 7, | ||
'cod_pais' : 8, | ||
'nome_pais' : 9, | ||
'cod_nat_juridica' : 10, | ||
'data_inicio_ativ' : 11, | ||
'cnae_fiscal' : 12, | ||
'tipo_logradouro' : 13, | ||
'logradouro' : 14, | ||
'numero' : 15, | ||
'complemento' : 16, | ||
'bairro' : 17, | ||
'cep' : 18, | ||
'uf' : 19, | ||
'cod_municipio' : 20, | ||
'municipio' : 21, | ||
'ddd_1' : 22, | ||
'telefone_1' : 23, | ||
'ddd_2' : 24, | ||
'telefone_2' : 25, | ||
'ddd_fax' : 26, | ||
'num_fax': 27, | ||
'email' : 28, | ||
'qualif_resp' : 29, | ||
'capital_social' : 30, | ||
'porte' : 31, | ||
'opc_simples' : 32, | ||
'data_opc_simples' : 33, | ||
'data_exc_simples' : 34, | ||
'opc_mei' : 35, | ||
'sit_especial' : 36, | ||
'data_sit_especial' : 37 | ||
} | ||
|
||
return header[column] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import settings | ||
import csv, codecs, os | ||
from header_dict import get_header_index | ||
|
||
location = os.getenv('FILES_LOCATION') | ||
|
||
file = 'empresas.csv' | ||
|
||
with codecs.open(location + file,'r+','utf-8') as empresas: | ||
|
||
empresas = csv.reader(cleaned.replace('\0', '') for cleaned in empresas) | ||
|
||
next(empresas,None) | ||
|
||
count_sucesso = 0 | ||
count_erro = 0 | ||
|
||
if not os.path.exists(location + 'UFs/'): | ||
os.makedirs(location + 'UFs/') | ||
|
||
for empresa in empresas: | ||
|
||
uf = empresa[get_header_index('uf')] | ||
municipio = empresa[get_header_index('municipio')] | ||
|
||
if not os.path.exists(location + 'UFs/' + uf): | ||
os.makedirs(location + 'UFs/' + uf) | ||
|
||
try: | ||
uf_file = open(location + 'UFs/' + uf + "/" + municipio + ".csv", "a+") | ||
except: | ||
uf_file = open(location + 'UFs/' + uf + "/" + municipio + ".csv", "w+") | ||
|
||
line = '"' + '","'.join(empresa) + '"\n' | ||
|
||
uf_file.writelines(line) | ||
|
||
try: | ||
uf_file.writelines('"' + '","'.join(empresa) + '"\n') | ||
count_sucesso += 1 | ||
except: | ||
count_erro += 1 | ||
print('Erros de processamento: {}'.format(count_erro),end='\r') | ||
|
||
print('Empresas processadas: {}'.format(count_sucesso),end='\r') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import settings | ||
import csv, codecs, os | ||
from header_dict import get_header_index | ||
|
||
location = os.getenv('FILES_LOCATION') | ||
|
||
file = 'empresas.csv' | ||
|
||
with codecs.open(location + file, 'r+', 'utf-8') as empresas: | ||
|
||
empresas = csv.reader(x.replace('\0', '') for x in empresas) | ||
|
||
next(empresas,None) | ||
|
||
count_sucesso = 0 | ||
count_erro = 0 | ||
|
||
if not os.path.exists(location + 'UFs/'): | ||
os.makedirs(location + 'UFs/') | ||
|
||
for empresa in empresas: | ||
|
||
uf = empresa[get_header_index('uf')] | ||
|
||
try: | ||
uf_file = open(location + 'UFs/' + uf + ".csv", "a+") | ||
except: | ||
uf_file = open(location + 'UFs/' + uf + ".csv", "w+") | ||
|
||
try: | ||
uf_file.writelines('"' + '","'.join(empresa) + '"\n') | ||
count_sucesso += 1 | ||
except: | ||
count_erro += 1 | ||
print('Erros de processamento: {}'.format(count_erro), end='\r') | ||
|
||
print('Empresas processadas: {}'.format(count_sucesso), end='\r') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# settings.py | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
# OR, the same with increased verbosity: | ||
load_dotenv(verbose=True) | ||
|
||
# OR, explicitly providing path to '.env' | ||
from pathlib import Path # python3 only | ||
env_path = Path('.') / '.env' | ||
load_dotenv(dotenv_path=env_path) |