首先我们要了解对Excel的读取操作步骤: (1)导入模块xlrd (2)打开工作簿Book (3)指定工作簿中的表单Sheet (4)根据行列序号读取内容
代码如下:
# (1)导入模块xlrd import xlrd # (2)打开工作簿Book wb = xlrd.open_workbook('../Stu_pack/school.xls') # (3)指定工作簿中的表单Sheet sheet = wb.sheet_by_index(0) # (4)根据行列序号读取内容 schools = [] # 定义一个二维列表 for row in range(sheet.nrows): school = [] # 定义一个一维列表存放单元格内容 for col in range(sheet.ncols): content = sheet.cell_value(row, col) school.append(content) # 将单元格的内容追加到一维列表里面 schools.append(school) # 将每行的内容追加到二维列表里面 # 从列表中读取前12行数据: i = 1 for school in schools: if i < 12: print(school) i = i + 1
结果如下: