Reputation: 137
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.
Upvotes: 1
Views: 7133
Reputation: 1386
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])
Upvotes: 1
Reputation: 34677
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...
Upvotes: 1