Python 之 Excel 数据处理
一.背景。
运维工作中,可能会遇到同事或者技术领导给Excel 数据进行,数据抽取汇总或者进行运维自动化提供元数据使用,针对以上场景我们需要进行python 处理,
二、Xlrd 模块介绍;
2.1 读Excel
Python语言中 xlrd是读取excel表格数据,支持 xlsx和xls 格式的excel表格;读取Excel的扩展工具。只能读。
2.2 写Excel
若写入,要用xlwt,意为:xls文件write写入库。)
可以实现指定表单、指定单元格的读取。
2.3 python3 三方模块安装方式:pip3 install xlrd,模块导入方式:import xlrd
2.4 相关参考链接:
三. Python 读Excel 实战脚本;
3.1 原Excel 数据格式;
3.2 python 读excel 代码部分;
def readExcel():
import xlrd
workbook=xlrd.open_workbook(r'/chj/devops/python/excel/file/app.xls')
sheet_name = workbook.sheet_names()
sheet = workbook.sheet_by_index(0)
# sheet索引从0开始
data=[]
rows = sheet.row_values(0)
for i in list(range(2,sheet.nrows)):
machineInfo=sheet.row_values(i)
if machineInfo[2] == "ptest":
data.append(machineInfo)
return data
resultData=readExcel()
print(resultData)
3.3 处理结构数据格式;
三. Python 写Excel (后期奉上,目前没时间进行整理)