TypeError: unsupported type for timedelta days component: unicode

John Machin sjmachin at lexicon.net
Fri Oct 6 08:13:35 EDT 2006


kath wrote:
> Hi,
>
> the following shows the contents of "datebook.xls"
>
> Date
> 8/9/2006
> 8/9/2006
> 8/9/2006
> 8/9/2006
> 8/9/2006
>
> # read_date.py
> import xlrd
>
> book = xlrd.open_workbook("datebook.xls")
> sh = book.sheet_by_index(0)
> ex_qdate=sh.cell_value(rowx=1,colx=0)
> pyd=datetime.date(1900,1,1)+datetime.timedelta(days=sh.cell_value(rowx=1,colx=0))

############################
Go back and read *ALL* the responses to your previous question. The
above is *INCORRECT*. As you can see, for input of 8/9/2006 (M/D/YYYY
format), it produces 2006-08-11.
*Please* read the sections on Excel dates in the xlrd documentation.
############################

> print pyd
>
> for c in range(sh.ncols):
>     for r in range(sh.nrows):
>         print sh.cell_value(rowx=r,colx=c)

Problem 1: you are including the heading.
Add this in here:
           print "row %d, col %d, cell_type %d" % (r, c,
sh.cell_type(r, c))
and that might give you a clue.

>         print
> datetime.date(1900,1,1)+datetime.timedelta(days=sh.cell_value(rowx=r,colx=c))

Maybe Problem 2: your posting seems to be missing indentation on this
line.

>
> ---- Output ----
> >>>
> 2006-08-11
> Date
> Traceback (most recent call last):
>   File "D:/Python23/Testing area/Python and Excel/read_date.py", line

How do you manage to get both "D:" and "/" in a path? Which version of
which OS are you running?

Why are you using Python 2.3?

Why are you keeping your own files in a subdirectory of your Python
installation?

> 12, in ?
>     print
> datetime.date(1900,1,1)+datetime.timedelta(days=sh.cell_value(rowx=r,colx=c))
> TypeError: unsupported type for timedelta days component: unicode
> >>>
>
> The first print statement gives me desired output. Whats wrong with
> second print statement?
> When I give the discriptor or index explicitly its giving the correct
> answer.
> Why not in the loop?

As explained above.

>
> Am I wrong some where?

I think that's not an reasonable conclusion :-)

HTH,
John




More information about the Python-list mailing list