Question

I am using a Python script with the xlrd module to read dates from an Excel file I have saved on my computer. Now, I already know that Excel saves dates as a serial date, which counts the number of days since Jan 0, 1900. I have been using the datetime function to convert this number into a date object with the form YYYY-MM-DD. For example:

v_created                     = sh.cell(r,created_col).value
if not v_created:
    v_created = 0
elif not isinstance(v_created, basestring):
    v_created = datetime.date(1900, 1, 1) + datetime.timedelta(int(v_created)-2)
print 'Create '
print v_created

This prints the following output:

Create 
2013-09-26 

On the other hand, the next block of code should do the exact same thing, but puts a float into the variable instead of a date:

v_updated                     = sh.cell(r,updated_col).value
if not v_updated:
    v_updated = 0
elif not isinstance(v_updated, basestring):
    v_upated = datetime.date(1900, 1, 1) + datetime.timedelta(int(v_updated)-2)
print 'Updated '
print v_updated  

As far as I can tell, this block of code is identical to the first, but spits out the following output:

Updated 
41543.5895833

I am sending these values to an Oracle database. When I run the query, I get the following error:

Traceback (most recent call last):
    cursor.execute(query, values)
cx_Oracle.DatabaseError: ORA-00932: inconsistent datatypes: expected DATE got NUMBER

Why is one block of code outputting a date object while the very next block of code outputting a float? As far as I can tell, Excel is storing the dates in the exact same way with the same formatting options.

Was it helpful?

Solution

You have a typo.

You write:

v_upated = datetime.date(1900, 1, 1) + datetime.timedelta(int(v_updated)-2)

and then print v_updated

When I run it, it works 41543 is actually the number of days since 1900... like you calculated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top