-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerador-codigos.py
71 lines (51 loc) · 2.07 KB
/
generador-codigos.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
import sys
import xlrd
values = "values ('{}','{}','{}','{}','{}')"
query_string = "INSERT INTO {table} (neighborhood, district, state, city, zip_code)"
positions = [1,3,4,5,0]
def one_file(book, insert):
sheet_names = book.sheet_names()
sheet_names.remove('Nota')
file = open('sql/codigos_postales_todos_los_estados.sql', 'w')
for name in sheet_names:
sheet = book.sheet_by_name(name)
file.write('\n\n-- Codigos postales para {}\n\n'.format(name))
for index in range(sheet.nrows):
if index is 0:
continue
row = sheet.row(index)
required_data = [row[i] for i in positions]
inserted_values = values.format( *( str(cell.value) for cell in required_data ) )
query = "{} {};\n".format(insert, inserted_values)
file.write(query)
file.close()
def multifile(book, insert):
sheet_names = book.sheet_names()
sheet_names.remove('Nota')
for name in sheet_names:
file = open('sql/{}.sql'.format(name), 'w')
sheet = book.sheet_by_name(name)
file.write('\n\n-- Codigos postales para {}\n\n'.format(name))
for index in range(sheet.nrows):
row = sheet.row(index)
inserted_values = values.format( *( str(cell.value) for cell in row ) )
query = "{} {};\n".format(insert, inserted_values)
file.write(query)
file.close()
def main(archivos_separados=False, **kwargs):
print("Cargando archivo, esto puede tardar un poco...\n\n\n")
if 'table' not in kwargs:
raise Exception('El parámetro table es requerido')
insert = query_string.format(table=kwargs['table'])
book = xlrd.open_workbook('source/CPdescarga.xls')
if archivos_separados:
multifile(book, insert)
else:
one_file(book, insert)
if __name__=='__main__':
args = sys.argv[1:]
dict_args = dict(arg.split('=') for arg in sys.argv[1:] if '=' in arg)
if '--archivos-separados' in args:
main(**dict_args, archivos_separados=True)
else:
main(**dict_args)