By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

TypeError: unsupported type for timedelta days component: unicode

P: n/a
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(day s=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.

Oct 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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(day s=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

Oct 6 '06 #2

P: n/a
John Machin wrote:
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>

Oct 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.