从excel for python中读取特定列

时间:2017-02-05 22:16:16

标签: python excel xlrd

import xlrd

workbook = xlrd.open_workbook(filename)
sheet = workbook.sheet_by_index(0)

array = []
for i in range(2, 9):
    array.append([sheet.cell(i, j).value for j in range(2, 5)])

Excel Image

我有这个代码,它工作正常,但它没有做我想要它做的事情。它从该excel文件的所有三列中提取数据(请参阅excel图像)。我只希望它从列C和列E中提取数据,并将其作为一对存储在数组中。怎么做?我知道在python中有跳过列和跳过行的东西,但不知道如何在我的代码中嵌入它。

1 个答案:

答案 0 :(得分:0)

使用openpyxl: -

def iter_rows(ws):
    result=[]
    for row in ws.iter_rows():
        rowlist = []
        for cell in row:
            rowlist.append(cell.value)
        result.append(rowlist)
    return result

wb = load_workbook(filename = '/home/piyush/testtest.xlsx')
ws = wb.active
first_sheet = wb.get_sheet_names()[0]
print first_sheet
worksheet = wb.get_sheet_by_name(first_sheet)

fileList =  (list(iter_rows(worksheet)))
col1 = []
col2 = []

for col in fileList:
    col1.append(col[1])#1 is column index
    col2.append(col[2])#2 is column index

for a in zip(col1,col2):
    print a
    #append as pair in another array

使用pandas: -

xl = pd.ExcelFile("/home/piyush/testtest.xlsx")
df = xl.parse("Sheet1")
df.iloc[:,[col1Index,col1Index]]