Commit 0a4e06201140b529c6436a7c27ec3cce03fc78d4
1 parent
bc80e898
Exists in
master
Adição de pasta lib para evitar conflito no merge
git-svn-id: http://svn.brlight.net/svn/lightbase-neo/trunk/LBBulk@905 29b92fdf-8c97-4584-b987-84e8d3c556fa
Showing
2 changed files
with
94 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +__author__ = 'eduardo' |
| @@ -0,0 +1,93 @@ | @@ -0,0 +1,93 @@ | ||
| 1 | +__author__ = 'eduardo' | ||
| 2 | +import csv | ||
| 3 | +import json | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +class CSVFileHandler: | ||
| 7 | + """ | ||
| 8 | + Classe para trabalhar com arquivos CSV | ||
| 9 | + """ | ||
| 10 | + def __init__(self, filename, outfile, delimiter=';', quotechar='"', as_dict=True, fieldnames=None): | ||
| 11 | + """ | ||
| 12 | + O Construtor abre o arquivo para leitura e inicia os parâmetros obrigatórios | ||
| 13 | + """ | ||
| 14 | + self.outfile = outfile | ||
| 15 | + self.delimiter = delimiter | ||
| 16 | + self.quotechar = quotechar | ||
| 17 | + self.as_dict = as_dict | ||
| 18 | + self.fieldnames = fieldnames | ||
| 19 | + self.filename = filename | ||
| 20 | + self.open(filename) | ||
| 21 | + | ||
| 22 | + def __del__(self): | ||
| 23 | + """ | ||
| 24 | + Fechar o arquivo é suficiente? | ||
| 25 | + """ | ||
| 26 | + self.close() | ||
| 27 | + | ||
| 28 | + def open(self, filename): | ||
| 29 | + """ | ||
| 30 | + Abrir o arquivo | ||
| 31 | + """ | ||
| 32 | + self.file = open(filename, 'r') | ||
| 33 | + if self.as_dict: | ||
| 34 | + self.reader = csv.DictReader(self.file, | ||
| 35 | + fieldnames=self.fieldnames, | ||
| 36 | + delimiter=self.delimiter, | ||
| 37 | + quotechar=self.quotechar) | ||
| 38 | + else: | ||
| 39 | + self.reader = csv.reader(self.file, | ||
| 40 | + delimiter=self.delimiter, | ||
| 41 | + quotechar=self.quotechar) | ||
| 42 | + | ||
| 43 | + def close(self): | ||
| 44 | + self.file.close() | ||
| 45 | + | ||
| 46 | + def process(self, function, args): | ||
| 47 | + """ | ||
| 48 | + Processa o arquivo carregado | ||
| 49 | + """ | ||
| 50 | + #for row in self.reader: | ||
| 51 | + # function(row, args) | ||
| 52 | + function(args) | ||
| 53 | + | ||
| 54 | + def csv2json(self): | ||
| 55 | + """ | ||
| 56 | + Converte o arquivo CSV de entrada em um JSON | ||
| 57 | + | ||
| 58 | + Fonte: http://stackoverflow.com/questions/1884395/csv-to-json-script | ||
| 59 | + """ | ||
| 60 | + # Normaliza o nome dos campos primeiro | ||
| 61 | + self.normalize() | ||
| 62 | + out = [obj for obj in self.reader] | ||
| 63 | + | ||
| 64 | + if out: | ||
| 65 | + with open(self.outfile, 'w+') as json_file: | ||
| 66 | + json_file.write(json.dumps(out)) | ||
| 67 | + else: | ||
| 68 | + # Adiciona alguma mensagem de erro | ||
| 69 | + print("ERRO - Erro ao processar o arquivo CSV") | ||
| 70 | + | ||
| 71 | + def normalize(self): | ||
| 72 | + fieldnames = list() | ||
| 73 | + for linha in self.reader: | ||
| 74 | + # Lê somente a primeira linha | ||
| 75 | + campos = linha | ||
| 76 | + break | ||
| 77 | + i = 0 | ||
| 78 | + for chave in campos: | ||
| 79 | + # O tamanho maximo do nome do campo é 10 | ||
| 80 | + nome = self.cap(chave, 10).strip() | ||
| 81 | + # Adiciona ao nome o número do campo | ||
| 82 | + nome += "_"+str(i) | ||
| 83 | + fieldnames.append(nome) | ||
| 84 | + i += 1 | ||
| 85 | + # Novo nome do campo | ||
| 86 | + self.fieldnames = fieldnames | ||
| 87 | + | ||
| 88 | + # Abre e fecha o arquivo de novo | ||
| 89 | + self.close() | ||
| 90 | + self.open(self.filename) | ||
| 91 | + | ||
| 92 | + def cap(self, s, l): | ||
| 93 | + return s if len(s)<=l else s[0:l] | ||
| 0 | \ No newline at end of file | 94 | \ No newline at end of file |