user1058210
user1058210

Reputation: 1689

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.

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']

Upvotes: 3

Views: 5601

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308548

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

Upvotes: 6

Joran Beasley
Joran Beasley

Reputation: 114108

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 :)

Upvotes: 8

Related Questions