Python 之 Excel 數據處理 python
一.背景。app
運維工做中,可能會遇到同事或者技術領導給Excel 數據進行,數據抽取彙總或者進行運維自動化提供元數據使用,針對以上場景咱們須要進行python 處理,運維
2、Xlrd 模塊介紹;ide
2.1 讀Excel
工具
Python語言中 xlrd是讀取excel表格數據,支持 xlsx和xls 格式的excel表格;讀取Excel的擴展工具。只能讀。excel
2.2 寫Excel索引
若寫入,要用xlwt,意爲:xls文件write寫入庫。)ip
能夠實現指定表單、指定單元格的讀取。文檔
2.3 python3 三方模塊安裝方式:pip3 install xlrd,模塊導入方式:import xlrdget
2.4 相關參考連接:
https://pypi.org/project/xlrd/ 官網
https://www.jianshu.com/p/f2c9dff344c6 以爲介紹的還不錯的文檔
三. 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 (後期奉上,目前沒時間進行整理)