Python: xlrd discerning dates from floats

六月ゝ 毕业季﹏ 提交于 2019-12-04 03:21:32

I think you could make this much simpler by making more use of the 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 a tuple.
    dt_tuple = xlrd.xldate_as_tuple(cell_value, workbook.datemode)
    # Create datetime object from this 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)

Well, never mind, I found a solution and here it is!

try:
    cell = worksheet.cell(row - 1, i)
    if cell.ctype == xlrd.XL_CELL_DATE:
        date = datetime.datetime(1899, 12, 30)
        get_ = datetime.timedelta(int(worksheet.cell_value(row - 1, i)))
        get_col2 = str(date + get_)[:10]
        d = datetime.datetime.strptime(get_col2, '%Y-%m-%d')
        get_col = d.strftime('%d-%m-%Y')
    else:
        get_col = unicode(int(worksheet.cell_value(row - 1, i))) 
except:
    get_col = unicode(worksheet.cell_value(row - 1, i))

A bit of explanation: it turns out that with xlrd you can actually check the type of a cell and check if it's a date or not. Also, Excel seems to have a strange way to save daytimes. It saves them as floats (left part for days, right part for hours) and then it takes a specific date (1899, 12, 30, seems to work OK) and adds the days and hours from the float to create the date. So, to create the date that I wanted, I just added them them and kept only the 10 first letters ([:10]) to get rid of the hours(00.00.00 or something...). I also changed the order of days_months-years because in Greece we use a different order. Finally, this code also checks if it can convert a number to an integer(I don't want any floats to show at my program...) and if everything fails, it just uses the cell as it is(in cases there are strings in the cells...). I hope that you find that useful, I think there are other threads that say that this is impossible or something...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!