def GuardarTabla(self, tbl, ruta_csv): sep = ',' quo = '"' with open(ruta_csv, 'wb') as io: columnas = [ self.AUnicode(tbl.horizontalHeaderItem(i).text()) for i in range(tbl.columnCount()) ] header = sep.join( [quo + c.replace(quo, quo * 2) + quo for c in columnas]) io.write(Encoding().ToString(header + '\n')) for f in range(tbl.rowCount()): fila = [] for c in range(tbl.columnCount()): item = tbl.item(f, c) fila.append( self.AUnicode(item.text()) if item is not None else '') datos = sep.join( [quo + d.replace(quo, quo * 2) + quo for d in fila]) io.write(Encoding().ToString(datos + '\n'))
def RealizarQueries(self, queries): try: conn = self.Conexion() curs = self.Cursor(conn) progreso = Progreso(1, len(queries), 'Ejecutando Querys') for query in queries: curs.execute(query) if query.strip().upper().startswith('SELECT'): datos = curs.fetchall() columnas = Encoding().NormalizarLista( [d[0] for d in curs.description]) conn.commit() else: datos = [] columnas = [] conn.commit() progreso.Incrementar() curs.close() conn.close() return [columnas, datos, None] except Exception as e: return [None, None, '[EXCEPTION] ' + str(e)]
def RepararEncoding(self, normalizar=False): with IOLector(self).Abrir(binario=True) as ior: datos = ior.Leer() #volar caracteres nulos datos = datos.replace('\x00', '') #volar prefijos unicode que joden boms = [ '\xef\xbb\xbf', '\xfe\xff', '\xff\xfe', '\x00\x00\xff\xfe', '\xff\xfe\x00\x00' ] for bom in boms: if datos.startswith(bom): datos = datos[len(bom):] #volar strings unicode que joden ustrs = ['\xef\xbf\xbd'] for ustr in ustrs: datos = datos.replace(ustr, '<?>') #normalizar caracteres extendidos if normalizar: datos = Encoding().NormalizarTexto(datos) with IOEscritor(self).Abrir(False) as iow: iow.Escribir(datos)
def EscribirDatos(self, datos): progreso = Progreso(5, len(datos), '[ %s ] Escribiendo' % (self._archivo)) for dato in datos: progreso.Incrementar() dato = Encoding().NormalizarLista(dato) self._writer.writerow(dato)
def XPathSafeProp(self, elem, path, prop, default=''): try: res = elem.xpath(path) if not res: return default txt = Encoding().NormalizarTexto(res[0].get(prop)) return txt except Exception as e: IOSistema().PrintLine(e) return '[ERROR-xpathprop]'
def EscribirHeader(self): if self._headerstruc is None: raise RuntimeError('[ %s ] Error: El _headerstruc no ha sido definido' % (self._archivo)) header = [] for tdato in self._headerstruc: header.append(tdato.Nombre().lower()) header = Encoding().NormalizarLista(header) self._writer.writerow(header)
def XPathSafeText(self, elem, paths, default=''): try: if type(elem) in [str, bytes] or any([ t in str(type(elem)) for t in ['_ElementStringResult', '_ElementUnicodeResult'] ]): return Encoding().NormalizarTexto(elem) if not type(paths) == list: paths = [paths] for path in paths: res = elem.xpath(path) if not res: continue txt = Encoding().NormalizarTexto(res[0]).strip() if not txt: continue return txt return default except Exception as e: IOSistema().PrintLine(e) return '[ERROR-xpathtext]'
def LeerRegistros(self): info = {'reg_repetidos':[], 'reg_erroneos':[], 'info_repetidos':[], 'info_erroneos':[], } if not self._archivo.Existe(): IOSistema().PrintLine('[ %s ] Error: El archivo no existe' % (self._archivo)) exit() self._datos.clear() self._archivo.RepararEncoding() progreso = Progreso(5, self.ContarRegistros(), '[ %s ] Leyendo' % (self._archivo)) with IOLector(self._archivo).Abrir(binario=True) as ior: self._reader = csv.reader(ior.Stream(), delimiter=self._delim, quotechar=self._quote) for hdr in self._reader: self._header = Encoding().NormalizarLista([h.lower() for h in hdr]) break for dato in self._reader: try: progreso.Incrementar() reg = self.RegistroUsandoDato(dato) key = self._func_reg_id(reg) if key not in self._datos: self._datos[key] = [] else: info['reg_repetidos'].append(reg) info['info_repetidos'].append(key) self._datos[key].append(Encoding().NormalizarLista(dato)) except Exception as e: info['reg_erroneos'].append(reg) info['info_erroneos'].append(e) self._inicializado = True return info
def CargarTabla(self, tbl, columnas, filas): if not columnas: columnas = ['(vacio)'] tbl.setRowCount(len(filas)) tbl.setColumnCount(len(columnas)) tbl.setHorizontalHeaderLabels(columnas) for n, fila in enumerate(filas): for i, d in enumerate(fila): d_str = Encoding().ToUnicode(d, 'utf-8') if d else '' item = QtGui.QTableWidgetItem(d_str) item.setToolTip(d_str) tbl.setItem(n, i, item)
def tran(wb, sh, rownum): res = [] for cell in sh.row(rownum): try: if cell.ctype == xlrd.XL_CELL_DATE: val = datetime.datetime( *xlrd.xldate_as_tuple(cell.value, wb.datemode)) elif cell.ctype == xlrd.XL_CELL_NUMBER and cell.value.is_integer( ): val = int(cell.value) elif cell.ctype == xlrd.XL_CELL_TEXT: val = Encoding().NormalizarTexto(cell.value) else: val = cell.value except Exception as e2: val = '<ERROR_XLS>' finally: res.append(val) return res
def Escribir(self, msg, stdout=False, newline=False): if self.handle is None: IOSistema().PrintLine('(!) Error: el archivo %s no se ha abierto' % (self.ruta)) return self.lock_w.acquire() msg = Encoding().NormalizarTexto(msg) if stdout: IOSistema().PrintWith(msg, newline) #msg = '%s%s' % (msg, IOSistema().NewLine() if newline else '') msg = '%s%s' % (msg, '\n' if newline else '') msg = bytes(msg, encoding='utf-8') if self.EnBinario() else msg self.handle.write(msg) self.lock_w.release()