1. 程式人生 > >python提取內容並寫入到Excel

python提取內容並寫入到Excel

pytho win exc -1 author mesh a-z += adl

#! /usr/bin/env python
#_*_coding:utf-8_*_

‘‘‘
Created on 2017-5-12

@author: Win-1
‘‘‘
import re,os,time
import xlwt

#讀取SNRt值
def readSNRt():
hotimeList = []
SrcMacList = []
BMacList = []
timeList = []

FileOpen = open(‘20170521-3015_7_V175.85.log‘)
FileMsg = FileOpen.readlines()

for i in range(0,len(FileMsg)):
timeShow = re.findall(r‘\d*:\d*:\d*:\d*‘,FileMsg[i])
hotime = re.findall(r‘hotime=\d*‘,FileMsg[i])
SrcMac = re.findall(r‘SrcMac=\dx[a-zA-Z0-9-]+‘,FileMsg[i]) # 需要大小寫字母
BMac = re.findall(r‘BMac=\dx\d*‘,FileMsg[i]) # 同上一行

if hotime:
timeList.append(‘‘.join(timeShow)[:])
hotimeList.append(‘‘.join(hotime)[7:])
SrcMacList.append(‘‘.join(SrcMac)[7:])
BMacList.append(‘‘.join(BMac)[5:])

return timeList,hotimeList,SrcMacList,BMacList
FileOpen.close()

def writeSNRtRSSIt():
newTable = ‘20170522-3021_V175.188.xlsx‘
wb = xlwt.Workbook(encoding=‘utf-8‘)
ws = wb.add_sheet(‘20170522-3021_V175.188‘,cell_overwrite_ok=True) #創建表
headData = [‘Time‘,‘hotime‘,‘SrcMac‘,‘BMac‘]
for colnum in range(0,4):
ws.write(0,colnum,headData[colnum],xlwt.easyxf(‘font:bold on‘))

timeShow = items[0]
hotime = items[1]
SrcMac = items[2]
BMac = items[3]

index = 1
for i in range(len(hotime)):
if int(hotime[i])<50: #提取hotime大於50的值
print ‘hotime less 50‘
continue
ws.write(index,0,timeShow[i])
ws.write(index,1,hotime[i])
ws.write(index,2,SrcMac[i])
ws.write(index,3,BMac[i])
index+=1
wb.save(newTable)

if __name__ == ‘__main__‘:
items = readSNRt()
writeSNRtRSSIt()

python提取內容並寫入到Excel