首頁>技術>

方法一:讀excel檔案單元格資料

import xlrd

book = xlrd.open_workbook('fruit.xlsx')

print('sheet頁名稱:',book.sheet_names())

sheet = book.sheet_by_index(0)

rows = sheet.nrows

cols = sheet.ncols

print('該工作表有%d行,%d列.'%(rows,cols))

print('第三行內容為:',sheet.row_values(2))

print('第二列內容為%s,資料型別為%s.'%(sheet.col_values(1),type(sheet.col_values(1))))

print('第二列內容為%s,資料型別為%s.'%(sheet.col(1),type(sheet.col(1))))

print('第二行第二列的單元格內容為:',sheet.cell_value(1,1))

print('第三行第二列的單元格內容為:',sheet.cell(2,1).value)

print('第五行第三列的單元格內容為:',sheet.row(4)[2].value)

print('第五行第三列的單元格內容為%s,資料型別為%s'%(sheet.col(2)[4].value,type(sheet.col(2)[4].value)))

print('第五行第三列的單元格內容為%s,資料型別為%s'%(sheet.col(2)[4],type(sheet.col(2)[4])))

方法二:讀excel檔案單元格資料--openpyxl

import openpyxl

book = openpyxl.load_workbook('fruit.xlsx')

print('所有sheet頁名稱:',book.sheetnames)

sheet = book.worksheets[0]

sheet2 = book['Sheet1']

sheet3 = book[book.sheetnames[0]]

print('工作表名稱:',sheet3.title)

rows = sheet.max_row

cols = sheet.max_column

print('該工作表有%d行,%d列.'%(rows,cols))

print('該工作表的的第三行第二列的單元格內容為:%.2f',(sheet.cell(3,2).value))

以下為行列生成器

print(sheet.rows,sheet.columns)

for col in sheet.columns:

print(col)

for row in sheet.rows:

for i in row:

print(i.value,end=' ')

print()

獲取某一行或列的內容

for i in list(sheet.rows)[1]:

print(i.value,end=' ')

print()

for i in list(sheet.columns)[0]:

print(i.value,end=' ')

11
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • CentOS7下搭建ClickHouse叢集