python提取内容并写入到Excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
以上是关于python提取内容并写入到Excel的主要内容,如果未能解决你的问题,请参考以下文章
Python 实现 PD 文字识别提取并写入 CSV 文件脚本分享
python 用于ExcelPython函数的Python装饰器。避免由Excel刷新机制导致的冗余Python函数调用。