TypeError: unsupported type for timedelta days component: unicode

K

kath

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))
print pyd

for c in range(sh.ncols):
for r in range(sh.nrows):
print sh.cell_value(rowx=r,colx=c)
print
datetime.date(1900,1,1)+datetime.timedelta(days=sh.cell_value(rowx=r,colx=c))

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

Am I wrong some where?


Please help me.
Thank you,
Sudhir.
 
J

John Machin

kath said:
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
 
F

Fredrik Lundh

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

IDLE defaults to the installation directory, so that's an easy thing
to do. and as long as you put it in a subdirectory, it's not much of a
problem either. (at least not until you want to uninstall Python...)

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top