你好,csv格式的和Excel格式的都是差不多的,
下面是读取Excel的一些函数,希望帮到你:
# -*- coding: cp936 -*-import xlrd3 def getAllRowsBySheetIndex(sheetIndex, xlsFilePath): workBook = xlrd3.open_workbook(xlsFilePath) table = workBook.sheets()[sheetIndex] rows = [] rowNum = table.nrows # 总共行数 rowList = table.row_values for i in range(rowNum): rows.append(rowList(i)) # 等价于rows.append(i, rowLists(i)) return rows def getRow(sheetIndex, rowIndex, xlsFilePath): rows = getAllRowsBySheetIndex(sheetIndex, xlsFilePath) return rows[rowIndex] def getAllColsBySheetIndex(sheetIndex, xlsFilePath): workBook = xlrd3.open_workbook(xlsFilePath) table = workBook.sheets()[sheetIndex] cols = [] colNum = table.ncols # 总共列数 colList = table.col_values for i in range(colNum): cols.append(colList(i)) return cols def getCol(sheetIndex, colIndex, xlsFilePath): cols = getAllColsBySheetIndex(sheetIndex, xlsFilePath) return cols[colIndex]def getCellValue(sheetIndex, rowIndex, colIndex, xlsFilePath): workBook = xlrd3.open_workbook(xlsFilePath) table = workBook.sheets()[sheetIndex] return table.cell(rowIndex, colIndex).value # 或者table.row(0)[0].value或者table.col(0)[0].value if __name__=='__main__': rowsInFirstSheet = getAllRowsBySheetIndex(0, './产品.xls') print(rowsInFirstSheet) colsInFirstSheet = getAllColsBySheetIndex(0, './产品.xls') print(colsInFirstSheet) print(getRow(0, 0, './产品.xls')) # 获取第一个sheet第一行的数据 print(getCol(0, 0, './产品.xls')) # 获取第一个sheet第一列的数据 print(getCellValue(0, 3, 2, './产品.xls')) # 获取第一个sheet第四行第二列的单元格的值
取消
评论