如何在Python中以Excel格式读取date?
如何将Exceldate(数字格式)转换为Python中的适当date?
你可以使用xlrd 。
从它的文档中 ,可以看到date总是以数字forms存储; 不过,您可以使用xldate_as_tuple
将其转换为pythondate。
注意:PyPI上的版本似乎比xlrd网站上的版本更新。
经过testing和几天等待反馈,我将在xlrd的xldate模块中svn-commit以下全新的函数…注意,它仍然不能用于仍然运行Python 2.1或2.2的顽固分子。
## # Convert an Excel number (presumed to represent a date, a datetime or a time) into # a Python datetime.datetime # @param xldate The Excel number # @param datemode 0: 1900-based, 1: 1904-based. # <br>WARNING: when using this function to # interpret the contents of a workbook, you should pass in the Book.datemode # attribute of that workbook. Whether # the workbook has ever been anywhere near a Macintosh is irrelevant. # @return a datetime.datetime object, to the nearest_second. # <br>Special case: if 0.0 <= xldate < 1.0, it is assumed to represent a time; # a datetime.time object will be returned. # <br>Note: 1904-01-01 is not regarded as a valid date in the datemode 1 system; its "serial number" # is zero. # @throws XLDateNegative xldate < 0.00 # @throws XLDateAmbiguous The 1900 leap-year problem (datemode == 0 and 1.0 <= xldate < 61.0) # @throws XLDateTooLarge Gregorian year 10000 or later # @throws XLDateBadDatemode datemode arg is neither 0 nor 1 # @throws XLDateError Covers the 4 specific errors def xldate_as_datetime(xldate, datemode): if datemode not in (0, 1): raise XLDateBadDatemode(datemode) if xldate == 0.00: return datetime.time(0, 0, 0) if xldate < 0.00: raise XLDateNegative(xldate) xldays = int(xldate) frac = xldate - xldays seconds = int(round(frac * 86400.0)) assert 0 <= seconds <= 86400 if seconds == 86400: seconds = 0 xldays += 1 if xldays >= _XLDAYS_TOO_LARGE[datemode]: raise XLDateTooLarge(xldate) if xldays == 0: # second = seconds % 60; minutes = seconds // 60 minutes, second = divmod(seconds, 60) # minute = minutes % 60; hour = minutes // 60 hour, minute = divmod(minutes, 60) return datetime.time(hour, minute, second) if xldays < 61 and datemode == 0: raise XLDateAmbiguous(xldate) return ( datetime.datetime.fromordinal(xldays + 693594 + 1462 * datemode) + datetime.timedelta(seconds=seconds) )
这里是裸指无座位安全带使用风险版本:
import datetime def minimalist_xldate_as_datetime(xldate, datemode): # datemode: 0 for 1900-based, 1 for 1904-based return ( datetime.datetime(1899, 12, 30) + datetime.timedelta(days=xldate + 1462 * datemode) )
xlrd.xldate_as_tuple
很好,但也有xlrd.xldate.xldate_as_datetime
转换为datetime。
import xlrd wb = xlrd.open_workbook(filename) xlrd.xldate.xldate_as_datetime(41889, wb.datemode) => datetime.datetime(2014, 9, 7, 0, 0)
请参阅此链接: 阅读date作为string不使用Python xlrd从Excel中浮动
它为我工作:
在拍摄中这个链接有:
import datetime, xlrd book = xlrd.open_workbook("myfile.xls") sh = book.sheet_by_index(0) a1 = sh.cell_value(rowx=0, colx=0) a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode)) print 'datetime: %s' % a1_as_datetime
因为你正在使用pandas和你的read_excel阅读date格式化为Excel数字不当,需要恢复背后的真实date…
在列上应用的lambda function
使用xlrd将date恢复
import xlrd df['possible_intdate'] = df['possible_intdate'].apply(lambda s: xlrd.xldate.xldate_as_datetime(s, 0)) >> df['possible_intdate'] dtype('<M8[ns]')
为了快速和肮脏:
year, month, day, hour, minute, second = xlrd.xldate_as_tuple(excelDate, wb.datemode) whatYouWant = str(month)+'/'+str(day)+'/'+str(year)
人员组合给了我excel转换的date和时间。 我确实把它作为一个string返回
def xldate_to_datetime(xldate): tempDate = datetime.datetime(1900, 1, 1) deltaDays = datetime.timedelta(days=int(xldate)) secs = (int((xldate%1)*86400)-60) detlaSeconds = datetime.timedelta(seconds=secs) TheTime = (tempDate + deltaDays + detlaSeconds ) return TheTime.strftime("%Y-%m-%d %H:%M:%S")
预期的情况
# Wrong output from cell_values() 42884.0 # Expected output 2017-5-29
示例:将图纸编号为0的 cell_values(2,2)作为目标date
获取所需的variables如下
workbook = xlrd.open_workbook("target.xlsx") sheet = workbook.sheet_by_index(0) wrongValue = sheet.cell_value(2,2)
并利用xldate_as_tuple
y, m, d, h, i, s = xlrd.xldate_as_tuple(wrongValue, workbook.datemode) print("{0} - {1} - {2}".format(y, m, d))
这是我的解决scheme
将excel文件转换为CSV时,date/时间单元格如下所示:
foo,2016/3/16 10:38,酒吧,
要将datetime文本值转换为datetime python对象,请执行以下操作:
from datetime import datetime date_object = datetime.strptime('3/16/2016 10:38', '%m/%d/%Y %H:%M') # excel format (CSV file)
打印date_object将返回2005-06-01 13:33:00