def from_csv(fp, field_names=None, **kwargs): fmtparams = {} for param in [ "delimiter", "doublequote", "escapechar", "lineterminator", "quotechar", "quoting", "skipinitialspace", "strict" ]: if param in kwargs: fmtparams[param] = kwargs.pop(param) if fmtparams: reader = csv.reader(fp, **fmtparams) else: dialect = csv.Sniffer().sniff(fp.read(1024)) fp.seek(0) reader = csv.reader(fp, dialect) table = PrettyTable(**kwargs) if field_names: table.field_names = field_names else: table.field_names = [x.strip() for x in next(reader)] for row in reader: table.add_row([x.strip() for x in row]) return table
def from_db_cursor(cursor, **kwargs): if cursor.description: table = PrettyTable(**kwargs) table.field_names = [col[0] for col in cursor.description] for row in cursor.fetchall(): table.add_row(row) return table
def generate_table(self, rows): """ Generates from a list of rows a PrettyTable object. """ table = PrettyTable(**self.kwargs) for row in self.rows: if len(row[0]) < self.max_row_width: appends = self.max_row_width - len(row[0]) for i in range(1, appends): row[0].append("-") if row[1]: self.make_fields_unique(row[0]) table.field_names = row[0] else: table.add_row(row[0]) return table
grupo_variaveis["qtd_img%d" % idx] = 0 for linha in f: if nomeTratado in linha: grupo_variaveis["qtd_msg%d" % idx] += 1 msgPura = regex.msgPura(linha).strip() grupo_variaveis["qtd_plv%d" % idx] += regex.contarPalavras(msgPura) grupo_variaveis["qtd_img%d" % idx] += calculo.quantasImg(msgPura) media = round( calculo.calcularMedia(grupo_variaveis["qtd_msg%d" % idx], grupo_variaveis["qtd_plv%d" % idx]), 2) grupo_variaveis["qtd_media%d" % idx] = media ##criação da tabela de output 1 zc = PrettyTable() zc.field_names = [ "Nome", "Qtd Mensagens", "Qtd Palavras", "Média", "Qtd imagens" ] for idx, nome in enumerate(dict_nomes, 1): zc.add_row([ nome, format(grupo_variaveis["qtd_msg%d" % idx], ",d"), format(grupo_variaveis["qtd_plv%d" % idx], ",d"), grupo_variaveis["qtd_media%d" % idx], format(grupo_variaveis["qtd_img%d" % idx], ",d") ]) print(zc) ##criação da tabela de output 2 listas = [[] for i in dict_nomes]