【问题标题】:How to determine the excel cell value type(data type such as date, string, float etc.)如何确定excel单元格值类型(日期、字符串、浮点数等数据类型)
【发布时间】:2019-12-31 06:33:18
【问题描述】:

我想读取excel表格中的cell(1,1)值,它是21-08-18格式的日期。 我正在使用 python 包xlrd 来读取值,它返回浮点值 ex。比如 4567.0

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

在读取值之前如何知道单元格的数据类型是datestringfloat

【问题讨论】:

    标签: python excel xlrd


    【解决方案1】:

    您可以获取单元格的类型以查看它是否为日期并将其与xlrd's cell object definition 进行比较(或直接将其与该表中的数字进行比较,因为 cell.ctype 只返回一个数字)。

    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!"
    

    【讨论】:

    • 这是我所期望的,谢谢@JiyuuSensei
    猜你喜欢
    • 2012-08-28
    • 2020-07-01
    • 2021-06-26
    • 2023-04-10
    • 2011-03-10
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多