-
Notifications
You must be signed in to change notification settings - Fork 3
/
paradoxdbsplitter.py
73 lines (57 loc) · 1.99 KB
/
paradoxdbsplitter.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import division
from pypxlib import Table
import csv
import math
import sys, getopt
def main(argv):
try:
opts, args = getopt.getopt(argv,"hi:o:c:x",["ifile=","ofile=","headers=","headershelp="])
except getopt.GetoptError:
print 'paradoxdbsplitter.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'paradoxdbsplitter.py -i <inputfile> -o <outputfile> -c <headers(hola,adios,pepe)>'
sys.exit()
elif opt in ("-x", "--helpheaders"):
headershelp = 1
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-c", "--headers"):
cabeceras = arg.split(",")
# elif opt in ("-b", "--block"):
# bloque = arg
bloque = 100000
table = Table(inputfile)
if 'headershelp' in locals():
print(table[0])
sys.exit()
registros = len(table)
print "Dimension filas tabla: %s." % registros
print "Cantidad de registros por fichero: %d." % bloque
print "Cabeceras que se van a usar: %s." % cabeceras
print "Ruta del archivo: %s." % inputfile
print registros
print bloque
print registros/bloque
print math.ceil(registros/bloque)
iteraciones = int(math.ceil(registros/bloque))
print "Ficheros que se van a generar: %s." % iteraciones
iteracion = 0
for iteracion in range(0, iteraciones):
print "iteracion numero %s" % iteracion
with open(outputfile+"_parte_"+str(iteracion)+".csv", "wb") as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
# headers
spamwriter.writerow(cabeceras)
start = iteracion * bloque
end = start + bloque
if end > registros:
end = registros
for row in range(start, end):
#print start
spamwriter.writerow([table[row][s].encode('utf8') if type(table[row][s]) is unicode else table[row][s] for s in cabeceras])
if __name__ == "__main__":
main(sys.argv[1:])