Day7 操作excel(读)

import xlrd
book=xlrd.open_workbook('new_stu.xls')#打开一个excel
sheet=book.sheet_by_index(0)#根据顺序获取sheet
sheet2=book.sheet_by_name('sheet1')#根据sheet页名字获取sheet

print(sheet.cell(0,0).value)#指定行和列获取数据

print(sheet.cell(0,1).value)#指定行和列获取数据

print(sheet.cell(0,2).value)#指定行和列获取数据

print(sheet.cell(0,3).value)#指定行和列获取数据

print(sheet.ncols)#获取excel里面有多少列
print(sheet.nrows)#获取excel里面有多少行

print(sheet.get_rows())

for i in range(sheet.nrows):#0,1,2,3,4,5
print(sheet.row_values(i))#取第几行的数据
for i in range(sheet.ncols):
print(sheet.col_values(i))

##################################

4

5

['姓名', '年龄', '性别', '分数']

['mary', 20.0, '女', 89.9]

['lucy', 50.0, '女', 100.0]

['CiCi', 10.0, '女', 89.9]

['Tom', 25.0, '男', 88.0]

['姓名', 'mary', 'lucy', 'CiCi', 'Tom']

['年龄', 20.0, 50.0, 10.0, 25.0]

['性别', '女', '女', '女', '男']

['分数', 89.9, 100.0, 89.9, 88.0]

你可能感兴趣的:(Day7 操作excel(读))