forked from IreneSL/pythonhack-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgestorDatos.py
More file actions
47 lines (40 loc) · 1.74 KB
/
Copy pathgestorDatos.py
File metadata and controls
47 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import psycopg2
import parametersConfig
import subprocess
host = parametersConfig.host
usuario = parametersConfig.usuario
password = parametersConfig.password
nombreBBDD = parametersConfig.bbdd
tablaBBDD = parametersConfig.tablaBBDD
def lectorDatos(fichero, delimitador, tipos):
# Codificación 'SECTOR'
convUTF8 = lambda valstr: str(valstr.decode("utf-8"))
# Para tratar reales con comas como separador de la parte decimal
convComaAPunto = lambda valstr: float(valstr.decode("utf-8").replace(',','.'))
c = {2:convUTF8, 5:convComaAPunto}
datos = np.genfromtxt(fichero, delimiter=delimitador, names=True, skip_header= 0, dtype=tipos, converters = c)
return datos
def insertaPostgreSQL(datos):
# Se supone creada la BBDD de nombre 'nombreBBDD'
# Se define la cadena de conexión
conn_string = "host=" + host + " dbname=" + nombreBBDD + " user=" + usuario + " password=" + password
print("Conectado a PostgreSQL\n ->%s" % (conn_string))
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
# Creación de la tabla
cursor.execute("CREATE TABLE " + tablaBBDD + "(CP_CLIENTE integer, CP_COMERCIO integer, SECTOR text, DIA DATE, FRANJA_HORARIA text, IMPORTE decimal, NUM_OP integer);")
# Inserciones
for tupla in datos:
query = "INSERT INTO " + tablaBBDD + " VALUES (%s, %s, %s, %s, %s, %s, %s)"
valores = (tupla['CP_CLIENTE'],tupla['CP_COMERCIO'],tupla['SECTOR'],tupla['DIA'],tupla['FRANJA_HORARIA'],tupla['IMPORTE'],tupla['NUM_OP'])
cursor.execute(query, valores)
cursor.close()
conn.commit()
conn.close()
datos = lectorDatos('datasets/card_analytics_dataset.txt','|',(int,int,'|S30','|S10','|S5',float,int))
print datos
print 'Filas: ', datos.shape[0]
insertaPostgreSQL(datos)