一、xlrd模块和xlwt模块是什么
xlrd模块是python第三方工具包,用于读取excel中的数据;
xlwt模块是python第三方工具包,用于往excel中写入数据;(只能创建新表格,不能修改表格内容!)
二、xlrd模块和xlwt模块的安装
pip install xlrd
pip install xlwt
三、Excel表格结构如下:
四、使用xlrd模块读取excel文件
#读取excel数据
def read_excel(self,excel_path,sheet_name):
xls = xlrd.open_workbook(excel_path,formatting_info=True) # 先打开已存在的表,formatting_info=True表示保留原表格的样式
sheet = xls.sheet_by_name(sheet_name) # 通过sheet名称获得sheet对象
dataList = []
for rows in range(1,sheet.nrows):#循环行
tempList = []
for cols in range(0,sheet.ncols-2):#循环列,因为最后两列是写入结果的所以减2
if cols==0:#判断如果是第一列则直接设置行数。
tempList.append(rows)
else:
tempList.append(sheet.cell_value(rows,cols))
dataList.append(tempList)
return dataList
read_excel方法参数说明:
exce