Python教程系列读取Excel数据
读取excel内容代码如下:
#导入操作excel文件的数据
import xlrd
#导入os模块 获取文件路径
import os
#创建读取文件的方法
def readexcel(name):
#读取要打开的文件
data=xlrd.open_workbook(name)
# 获取sheet页名称
print(data.sheet_names())
#获取指定的工作表
table=data.sheet_by_name('Sheet1')
#设置空列表
list=[]
#循环读取每行表的数据
for shuju in range(table.nrows):
#如果行数大于0
if shuju>0:
#像空列表中添加读取的每行的数据
list.append(table.row_values(shuju))
#返回读取的excel中所有行的数据
return list
if __name__ == '__main__':
#设置文件路径
excelFile = os.path.abspath(os.getcwd()) + '/ceshi.xlsx'
#调用并打印excel中的内容
print(readexcel(excelFile))
excel读取结果如下: