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

强颜欢笑 提交于 2019-12-13 05:11:44

问题


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?


回答1:


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'


来源:https://stackoverflow.com/questions/21948935/why-is-my-python-code-returning-textmy-string-instead-of-just-my-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!