Search code examples
pythonxlrd

why is my python code returning text:'my string' instead of just my string?


my code snippet looks like this:

for current_row in range(worksheet.nrows):
    fname_text = worksheet.row(current_row)[0]
    lname_text = worksheet.row(current_row)[1]
    cmt = worksheet.row(current_row)[2]
    print (fname_text, lname_text, cmt)

this prints:

text:'firstname' text:'lastname' text'the cmt line'

i want it just to return:

firstname lastname the cmt line

what do i need to change to make this happen?


Solution

  • That's what Cell objects look like:

    >>> sheet.row(0)
    [text:u'RED', text:u'RED', empty:'']
    >>> sheet.row(0)[0]
    text:u'RED'
    >>> type(sheet.row(0)[0])
    <class 'xlrd.sheet.Cell'>
    

    You can get at the wrapped values in a few ways:

    >>> sheet.row(0)[0].value
    u'RED'
    >>> sheet.row_values(0)
    [u'RED', u'RED', '']
    

    and remember you can access cells without going via row:

    >>> sheet.cell(0,0).value
    u'RED'