我不太确定如何使用以下功能:
xlrd.xldate_as_tuple
以下数据
xldate:39274.0
xldate:39839.0
有人可以给我一个关于数据功能使用的例子吗?
答案 0 :(得分:25)
Quoth the documentation:
Excel电子表格中的日期
实际上,没有这样的事情。 你有什么浮点 数字和虔诚的希望。有 Excel日期的几个问题:
(1)日期不作为单独存储 数据类型;它们存储为浮动 点数,你必须依靠 (a)适用的“数字格式” 他们在Excel中和/或(b)知道哪些 细胞应该有日期 他们。该模块有助于(a)通过 检查已经存在的格式 应用于每个数字单元;如果它 似乎是日期格式,单元格 被归类为日期而不是日期 数。对此功能的反馈, 特别是来自非英语国家 locales,将不胜感激。
(2)Excel for Windows存储日期 默认为天数(或 其中的一部分) 1899-12-31T00:00:00。 Excel for Macintosh使用默认的开始日期 1904-01-01T00:00:00。日期系统 可以在Excel上更改 每个工作簿的基础(例如:工具 - >选项 - >计算,勾选“1904日期系统”框。这是 如果已经存在,那当然是一个坏主意 工作簿中的日期。没有 有理由改变它即使在那里 在工作簿中没有日期。哪一个 日期系统正在使用中记录 工作簿。运送工作簿 从Windows到Macintosh(或副) 反之亦然)将正常使用 主持Excel。使用此模块时 xldate_as_tuple函数进行转换 您必须使用工作簿中的数字 本书的datemode属性 宾语。如果你猜,或做一个 判断取决于你在哪里 相信工作簿是创建的,你 冒出1462天的风险 良好状态。
参考: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q180162
(3)Excel的Excel实现 Windows默认的基于1900的日期系统 适用于不正确的前提 1900年是闰年。它解释 数字60表示1900-02-29, 这不是一个有效的日期。 因此任何数字少于61 很暧昧。例如:是59 直接进入1900-02-28的结果, 或者它是1900-03-01减去2天?该 OpenOffice.org Calc程序“更正” 微软的问题;进入 1900-02-27导致数字59 存储。保存为XLS文件,然后打开 使用Excel的文件 - 你会看到 1900-02-28显示。
参考: http://support.microsoft.com/default.aspx?scid=kb;en-us;214326
我在这里引用,因为你的问题的答案可能是错误的,除非你考虑到这一点。
所以把它放到代码中会是这样的:
import datetime
import xlrd
book = xlrd.open_workbook("myfile.xls")
sheet = book.sheet_by_index(0)
cell = sheet.cell(5, 19) # type, <class 'xlrd.sheet.Cell'>
if sheet.cell(5, 19).ctype == 3: # 3 means 'xldate' , 1 means 'text'
ms_date_number = sheet.cell_value(5, 19) # Correct option 1
ms_date_number = sheet.cell(5, 19).value # Correct option 2
year, month, day, hour, minute, second = xlrd.xldate_as_tuple(ms_date_number,
book.datemode)
py_date = datetime.datetime(year, month, day, hour, minute, nearest_second)
为您提供py_date
中的Python日期时间,您可以在使用标准datetime模块时执行有用的操作。
我从来没有使用过xlrd,我的例子已经完全弥补了,但是如果有一个myfile.xls
并且它在单元格F20中确实有一个日期编号,那么你对于精确度并不是很挑剔上面,这段代码应该可行。
答案 1 :(得分:8)
该功能的文档(减去可能的例外列表):
xldate_as_tuple(xldate,datemode)[#]
Convert an Excel number (presumed to represent a date, a datetime or a time) into a tuple suitable for feeding to datetime or mx.DateTime constructors. xldate The Excel number datemode 0: 1900-based, 1: 1904-based. 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. Returns: Gregorian (year, month, day, hour, minute, nearest_second).
作为xlrd的作者,我很想知道如何更好地制作文档。你能否回答一下:
您是否阅读过日期的一般部分(@msw引用)?
您是否阅读了上述功能的具体文档?
你能否在文件中提出任何改进建议?
你真的尝试过运行这个函数吗:
>>> import xlrd
>>> xlrd.xldate_as_tuple(39274.0, 0)
(2007, 7, 11, 0, 0, 0)
>>> xlrd.xldate_as_tuple(39274.0 - 1.0/60/60/24, 0)
(2007, 7, 10, 23, 59, 59)
>>>
答案 2 :(得分:1)
将其用作such:
number = 39274.0
book_datemode = my_book.datemode
year, month, day, hour, minute, second = xldate_as_tuple(number, book_datemode)
答案 3 :(得分:1)
import datetime as dt
import xlrd
log_dir = 'C:\\Users\\'
infile = 'myfile.xls'
book = xlrd.open_workbook(log_dir+infile)
sheet1 = book.sheet_by_index(0)
date_column_idx = 1
## iterate through the sheet to locate the date columns
for rownum in range(sheet1.nrows):
rows = sheet1.row_values(rownum)
## check if the cell is a date; continue otherwise
if sheet1.cell(rownum, date_column_idx).ctype != 3 :
continue
install_dt_tuple = xlrd.xldate_as_tuple((rows[date_column_idx ]), book.datemode)
## the "*date_tuple" will automatically unpack the tuple. Thanks mfitzp :-)
date = dt.datetime(*date_tuple)
答案 4 :(得分:0)
这是我用来自动转换日期的内容:
cell = sheet.cell(row, col)
value = cell.value
if cell.ctype == 3: # xldate
value = datetime.datetime(*xlrd.xldate_as_tuple(value, workbook.datemode))