python提取内容并写入到Excel

#! /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()

 

转载于:https://www.cnblogs.com/hubangsheng/p/6890918.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值