【问题标题】:Python xlrd read DateTime as serial numberPython xlrd 将 DateTime 读取为序列号
【发布时间】:2015-08-04 14:12:41
【问题描述】:

我有一个日期时间格式的 Excel。但是当我阅读表格时,它显示为序列号。

例如,在工作表中它是 08/03/2015,但 xlrd 读取为 42219

代码很简单

workbook = xlrd.open_workbook(file_contents=excel_contents)
the_sheet = workbook.sheet_names()[0]
number_of_rows = the_sheet.nrows - 1
curr_row = 0
data = []

while curr_row < number_of_rows:
    curr_row += 1
    air_date = the_sheet.cell_value(curr_row, 1)
    air_time = the_sheet.cell_value(curr_row, 2)
    schedule_time = '{}:{}'.format(air_date, air_time)
    unaware = datetime.strptime(schedule_time, '%m/%d/%Y:%I:%M %p') 

我用来在 Python 中翻译成 datetimeair_date 被炸掉了,因为格式不同。

这是excel示例。

这是我得到的错误

builtins.ValueError ValueError: 时间数据 '42219.0:06:00 AM' 没有 匹配格式 '%m/%d/%Y:%I:%M %p'

【问题讨论】:

    标签: python excel datetime xlrd


    【解决方案1】:

    看来您需要使用xldate_as_tuple 将excel 格式转换为python 格式(请参阅documentationthis already existing question,您可以在其中找到将日期转换为python 的格式。

    xldate_as_tuple:

    将 Excel 数字(假定表示日期、日期时间或时间)转换为适合提供给 datetime 或 mx.DateTime 构造函数的元组。

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 2011-02-13
      • 2017-05-25
      • 1970-01-01
      相关资源
      最近更新 更多