1

Please click here to view the Excel file

I want to retrieve headers of this excel file (Only A,B,C)and store it in a list using Python. I opened my file but I am unable to retrieve it.

import xlrd
file_location= "C:/Users/Desktop/Book1.xlsx"
workbook= xlrd.open_workbook(file_location)
sheet=workbook.sheet_by_index(0) 

Can anyone please help me with that? I am new to Python.
Thank you for your help.

4
  • are you open to use pandas library and maybe read the whole excel file easily ? Commented Jul 5, 2017 at 14:06
  • I can use anything but I don't want the rest of the data. Only headers Commented Jul 5, 2017 at 14:08
  • 1
    which row is the headers, I see two rows which could be them. Commented Jul 5, 2017 at 14:09
  • Updated the post. Only A,B, C Commented Jul 5, 2017 at 14:10

2 Answers 2

1

You could also try using the numpy method loadtxt: https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html Which gives you arrays for each column (you can also skip specified columns so that you only get the A,B,C as you desire). You can write a for loop to get the first entry of each column and put them in a list.

data = loadtxt("Book1.xlsx")
headers = [] 
for c in range(1,data.shape[0]): 
    if data[c, 0] != "": 
        headers.append(data[c, 0])
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, but I might add a couple of columns in future so I can't hard code the number. Can you syntactically tell me how do I do that?
You can do: data = loadtxt("Book1.xlsx") headers = [data[c,0] for c in range(data.shape[0] if data[c,0] != ""]
You can also skip the first column by changing the range arguments to range(1,data.shape[0])
I am getting following error:>>>>>>>>>>>>>>>>>>>>>> headers = [data[c,0] for c in range(1,data.shape[0]) if data[c,0] != ""] AttributeError: '_io.TextIOWrapper' object has no attribute 'shape'
Actually, I'm not sure the generator expression is smooth, so try this: headers = [] for c in range(1,data.shape[0]): if data[c, 0] != "": headers.append(data[c, 0])
|
1
iterheaders = iter(sheet.row(1))
headers = [str(cell.value) for cell in iterheaders[1:] if cell is not None and cell.value != '']

Hope that helps...

5 Comments

Hi, Thanks! This helped partially but the output is ['Date', '', 'A', '', '', 'B', '', '', 'C', ''] I want it look as [A, B, C]. Thanks.
Then try to do this instead: headers = [str(cell.value) for cell in iterheaders if cell is not None and cell != '']
Sorry that didn't work either! Still the same output
Try now @Programmer?
so, you accepted the answer without understanding it? Great... The update skips the first column and adds the check that @AndreasLympouras suggested in his comment

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.