我如何获取 excel 文件并将其列转换为 Python 中的列表?

How would I take an excel file and convert its columns into lists in Python?

我有以下问题。也就是说,假设我有一个 excel 文件,第一行有一些名字。然后接下来的许多行都是单字母条目(所以 "A"、"B"、"C" 等等)。

我的目标是提取该列并将其放入列表中,例如,对于第 1 列,列表将从第 2 行开始,下一个条目将从第 3 行开始,依此类推,直到到达终点。

在 Python 中我该怎么做?

我使用了一个名为 xlrd 的模块。

关于那个的一些信息http://www.blog.pythonlibrary.org/2014/04/30/reading-excel-spreadsheets-with-python-and-xlrd/

这是包裹:https://pypi.python.org/pypi/xlrd

要排除第一行,并为不同的列创建不同的列表,您可以执行类似...

from xlrd import open_workbook

book = open_workbook("mydata.xlsx")
sheet = book.sheet_by_index(0) #If your data is on sheet 1

column1 = []
column2 = []
#...

for row in range(1, yourlastrow): #start from 1, to leave out row 0
    column1.append(sheet.cell(row, 0)) #extract from first col
    column2.append(sheet.cell(row, 1))
    #...

将包含数据的最后一行的索引 1 放在 'yourlastrow' 占位符中。

我找到了答案。假设Excel文件是Test.xlsx,我们可以将第j列(不包括第一行)翻译成list_j如下:

book = xlrd.open_workbook("Test.xlsx")
sheet = book.sheet_by_index(0)

list_j = []

for k in range(1,sheet.nrows):
    list_j.append(str(sheet.row_values(k)[j-1]))