TypeError: unsupported type for timedelta days component: unicode

Discussion in 'Python' started by kath, Oct 6, 2006.

  1. kath

    kath Guest

    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.
     
    kath, Oct 6, 2006
    #1
    1. Advertising

  2. kath

    John Machin Guest

    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
     
    John Machin, Oct 6, 2006
    #2
    1. Advertising

  3. 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>
     
    Fredrik Lundh, Oct 6, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Koekenberg
    Replies:
    3
    Views:
    1,375
    Martin Koekenberg
    Aug 26, 2004
  2. Brett C.
    Replies:
    2
    Views:
    2,898
    Martin Koekenberg
    Aug 30, 2004
  3. Jay Parlar
    Replies:
    0
    Views:
    814
    Jay Parlar
    Mar 17, 2006
  4. Gilbert Fine
    Replies:
    8
    Views:
    932
    Zentrader
    Aug 1, 2007
  5. Jordan Harry
    Replies:
    1
    Views:
    516
    Sean DiZazzo
    May 2, 2008
Loading...

Share This Page