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 74 75 76 77 78 79 80 81
| def read_biometrie_pulli(self, conn, config):
i = 1
while (config.has_section('biometrie_pulli_' + str(i))):
biometrie_pulli_number = 'biometrie_pulli_' + str(i)
print biometrie_pulli_number
wb = open_workbook(config.get(biometrie_pulli_number, 'file').decode('UTF-8'))
sheet = wb.sheet_by_name(config.get(biometrie_pulli_number, 'sheet').decode('UTF-8'))
m = self.get_columnsNumber(config, sheet, biometrie_pulli_number)
items = config.items(biometrie_pulli_number)
for row in range(1, sheet.nrows):
print row
if sheet.cell(row, 0).ctype is not XL_CELL_BLANK and sheet.cell(row, 0).ctype is not XL_CELL_EMPTY:
self.logger.info(sheet.cell(row, 0).value)
try:
biometrie_pulli = dict()
for item in items:
key, value = item[0], item[1]
if key not in ('file', 'sheet'):
key, type = item[0], item[1].split(';')[1]
if type == 'date':
val = sheet.cell(row, m[key]).value
try:
val = xldate_as_tuple(val, wb.datemode)
val = datetime.date(val[0], val[1], val[2])
biometrie_pulli[key] = val
except:
pass
elif type == 'float':
val = sheet.cell(row, m[key]).value
try:
float(val)
biometrie_pulli[key] = val
except:
pass
else:
biometrie_pulli[key] = sheet.cell(row, m[key]).value
self.record_biometrie_pulli(conn, biometrie_pulli)
except Exception as e: # pass
print row
print e
self.logger.error("\t ERREUR SUR ligne : " + str(row))
self.logger.error("\t Message : " + e.message)
exit(0)
i = i + 1
def record_biometrie_pulli(self, conn, biometrie_pulli):
cur = conn.cursor()
keys = biometrie_pulli.keys()
query = u"insert into biometrie_pulli (" + ", ".join(keys) + ") values ("
values = list()
for key in keys:
val = biometrie_pulli[key]
try:
val = str(val)
except:
pass
val = val.replace('\'', '\'\' ')
values.append("'" + val + "'")
query += ", ".join(values) + ")"
self.logger.debug(query)
try:
cur.execute(query)
except Exception as e:
print query
print e
self.logger.error(query)
self.logger.error(e)
cur.close()
conn.commit() |
Partager