1

I am treating Excel sheets using Python. I wrote this :

import xlrd
wb=xlrd.open_workbook('C:\\Inputs_UNF_newForm.xlsx')
s=wb.sheet_by_name('Chemical treatments')

def getOutputs(s):
    for row_index in range(28):
        v = s.cell_value(row_index, 1)
        if s.cell_value(row_index, 1) != "" :
            print ('"'+v +'"'+","),

getOutputs(s)

It works just fine. (Well, except that there is still a coma after the last word..) The thing is, I need to create a list of the result of the getOuputs fonction, ie. a list of the cells I read.

I did this:

OutputList=[getOutputs(s)]
print OutputList[1]

But it didn't work. What am I missing? (The extra coma?)

1
  • getOutputs doesn't return anything (or rather, will implicitly return None), so you should not be surprised by this. Even if it did return something, there would never be an OutputList[1], because list indices are zero-based. Commented Feb 25, 2015 at 14:52

1 Answer 1

2

The problem is that you are just printing the values instead of returning them. Here I use an originally empty list keep and append the values to the keep list, then return that list to the caller.

def getOutputs(s):
    keep = []
    for row_index in range(28):
        v = s.cell_value(row_index, 1)
        if v != "" :
            keep.append(v)
    return keep

On a side note, an easy way to deal with printing the values without the trailing comma would then be `print(",".join(OutputList)).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your return. I used your solution and I added a print(getOuputs(s) and got [u'item1', u'item2', u'item3'] What does the 'u' mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.