歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python讀取excel表格統計不同網段IP個數

Python讀取excel表格統計不同網段IP個數

日期:2017/3/1 10:07:10   编辑:Linux編程

import xlrd


file = xlrd.open_workbook('demo.xls') #打開表格
sheet1 = file.sheet_by_name(u'hhc') #獲取表格


nrows = sheet1.nrows
dirt = {} #用於保存統計結果

#統計不同
for i in range(0, nrows):
tempCell = sheet1.cell(i,0).value #0表示ip記錄在第一列
temp = tempCell[:tempCell.rfind('.') ]
dirt.setdefault(temp, 0)
dirt[temp] += 1

#打印統計結果
for name,address in dirt.items():
print ('%s : %s'%(name,address))

使用表格

運行結果

Copyright © Linux教程網 All Rights Reserved