使用Python在Excel中检索标题

请点击这里查看Excel文件

我想检索这个excel文件的头(只有A,B,C),并使用Python将其存储在列表中。 我打开了我的文件,但我无法检索它。

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

任何人都可以帮助我吗? 我是Python的新手。
感谢您的帮助。

你也可以尝试使用numpy方法loadtxt: https ://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html它给你每个列的数组(你也可以跳过指定的列,让你只有你想要的A,B,C)。 你可以写一个for循环来获取每一列的第一个条目,并把它们放在一个列表中。

 data = loadtxt("Book1.xlsx") headers = [] for c in range(1,data.shape[0]): if data[c, 0] != "": headers.append(data[c, 0]) 
 iterheaders = iter(sheet.row(1)) headers = [str(cell.value) for cell in iterheaders[1:] if cell is not None and cell.value != ''] 

希望有帮助…