excel - Python: xlrd discerning dates from floats -
i wanted import file containing text, numbers , dates using xlrd on python.
i tried like:
if "/" in worksheet.cell_value: do_this else: do_that
but of no use latter discovered dates stored floats, not strings. convert them datetime type did:
try: get_row = str(datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell_value(i, col - 1), workbook.datemode))) except: get_row = unicode(worksheet.cell_value(i, col - 1))
i have exception in place when cell contains text. want numbers numbers , dates dates, because right numbers converted dates.
any ideas?
i think make simpler making more use of tools available in xlrd:
cell_type = worksheet.cell_type(row - 1, i) cell_value = worksheet.cell_value(row - 1, i) if cell_type == xlrd.xl_cell_date: # returns tuple. dt_tuple = xlrd.xldate_as_tuple(cell_value, workbook.datemode) # create datetime object tuple. get_col = datetime.datetime( dt_tuple[0], dt_tuple[1], dt_tuple[2], dt_tuple[3], dt_tuple[4], dt_tuple[5] ) elif cell_type == xlrd.xl_cell_number: get_col = int(cell_value) else: get_col = unicode(cell_value)
Comments
Post a Comment