简体   繁体   English

Python - 导入Excel日期转换为数字

[英]Python - Importing Excel dates converts to number

I'm trying to learn to the use the xlrd package in Python to read Excel files, and I have made a sample file which contains a list of chronological dates and in the second column the day of the week it corresponds to. 我正在尝试学习使用Python中的xlrd包来读取Excel文件,并且我已经制作了一个包含按时间顺序排列的日期列表的示例文件,并在第二列中创建了它对应的星期几。

The problem is that when I read in the data it displays it as a number. 问题是,当我读入数据时,它会将其显示为数字。 How can I get the date to display the way it is supposed to? 如何获取日期以显示其应有的方式?

[u'Date', u'Day']
[41162.0, u'Monday']
[41163.0, u'Tuesday']
[41164.0, u'Wednesday']
[41165.0, u'Thursday']
[41166.0, u'Friday']

you want 你要

wb = xlrd.open_workbook("somewb.xls")
my_date_tuple = xlrd.xldate_as_tuple(xls_timestamp_number,wb.datemode)

which then returns a date tuple that is much easier to work with :) 然后返回一个更容易使用的日期元组:)

>>> import datetime
>>> datetime.datetime(1899,12,30) + datetime.timedelta(days=int(41162.0))
datetime.datetime(2012, 9, 10, 0, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM