forked from oscarfonts/catastro2postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat2csv.py
executable file
·38 lines (31 loc) · 1.06 KB
/
cat2csv.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import csv
from catstruct import catstruct # define la estructura de un .CAT
rf = open('data/attr.cat') # input file
wf = {} # output files
# Generamos un CSV por cada tipo de registro
for tipo in catstruct:
filename = 'data/cat_'+str(tipo)+'.csv'
print 'Creando ' + filename;
wf[tipo] = open(filename, 'w')
# Escribimos primer registro con nombres de campos
csv.writer(wf[tipo]).writerow(zip(*catstruct[tipo])[3])
# Leemos .cat registro a registro
for line in rf.readlines():
line = line.decode("UTF8")
row = []
tipo = int(line[0:2])
# Parseamos si conocemos la estructura del tipo de registro
if tipo in catstruct:
for campos in catstruct[tipo]:
ini = campos[0]-1 # offset
fin = ini + campos[1] # longitud
valor = line[ini:fin].strip().encode("UTF8") # valor
row.append(valor)
csv.writer(wf[tipo]).writerow(row)
# close input file handle
rf.close()
# close output file handles
for f in wf:
wf[f].close()