Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Last active October 12, 2017 23:27
Show Gist options
  • Select an option

  • Save victorkurauchi/83fbef645217dbee303a057a2d6a9fc9 to your computer and use it in GitHub Desktop.

Select an option

Save victorkurauchi/83fbef645217dbee303a057a2d6a9fc9 to your computer and use it in GitHub Desktop.
Buscando resultados SQL (aqui considerando o .csv) via Python e obtendo ocorrências, e quantidade de reincidentes.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import Counter
from itertools import groupby, islice
import csv
# connection to database should be here
# .csv should be the result from database
#
# line example
# cod_Contrato|cod_Cluster_v25|nom_Cluster_V25|cod_Regional|cod_Produto|cod_Segmento|dat_Reclamacao|val_Reincidencia_EE_Pre_Dias|cod_Motivo|cod_Motivo_Reincidente|ind_Origem|ind_Migracao|ind_Repetitiva|val_Qtd
# |102|ANAPOLIS|4|2|1|2017-09-01 14:22:44|3|1||2|0||1
issues = open('Amostra_Reparos.csv', 'r')
issues_list = []
occurences_by_region = Counter(map(lambda line: line.split('|')[2], issues))
# print occurences_by_region
with issues as f:
reader = csv.reader(f)
first_line = f.readline()
headers = first_line.split('|')
for row in reader:
issue_object = {}
values = row[0].split('|')
for idx, val in enumerate(values):
issue_object[headers[idx]] = val
issues_list.append(issue_object)
# get issues with less than 30 days reincidence
# print 'issues_list length'
# print len(issues_list)
issues_with_reincidence = len(filter(lambda x: x['val_Reincidencia_EE_Pre_Dias'] and int(x['val_Reincidencia_EE_Pre_Dias']) < 30, issues_list))
# print issues_with_reincidence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment