如何判断excel单元格的值类型(日期、字符串、浮点等数据类型)

How to determine the excel cell value type(data type such as date, string, float etc.)

我想读取excelsheet中的cell(1,1)值,是21-08-18格式的日期。 我正在使用 python 包 xlrd 来读取值,它 returns 浮点值 ex。像 4567.0

book = xlrd.open_workbook(path)
compSheet = book.sheet_by_name("sheet_name")
cell_value = compSheet.cell_value(1, 1)

如何在读取值之前知道单元格的数据类型是date还是string还是float等等

您可以获取单元格的类型以查看它是否是日期并将其与 xlrd's cell object definition 进行比较(或者直接将其与 table 中看到的数字进行比较,如 cell.ctype只是 returns 个数字)。

book = xlrd.open_workbook(path)
compSheet = book.sheet_by_name("sheet_name")
cell = compSheet.cell(1,1)
if cell.ctype is xlrd.XL_CELL_DATE:
    print "Cell 1,1 is a date!"