天天看点

python匹配字符串并写入txt文件_Python扫描txt文件中的字符串,然后写入Excel,然后继续进行下一个匹配...

感谢你先阅读这篇文章。我通常用Perl编写代码,但我将转向Python,我正试图用下面的脚本应付过去。在

我有一个大的文本文件,其格式如下。。。在2014 Apr 11 07:14:03.155 sectorBLAH

Interestingcontent

Interesting1 = 843

Interesting2 = 56

ReallyInteresting = 1

Interesting3 = N/A

2014 Apr 11 07:14:04.189 sectorBLAH

Interestingcontent

Interesting1 = 7843

Interesting2 = 656

ReallyInteresting = 0

Interesting3 = 5

这个序列会持续一段时间。

我希望做的是创建一个xls文件,在我浏览这个长列表时填充它。基本上,我希望找到“reallyinterest”字符串并捕获它的值“0”或“1”,然后将该值发送到xls中的第2列。同时,在xls的第1列中,我希望发布与“reallyinterest”字符串组的输出相关联的时间戳,有点像这样。。。在

^{pr2}$

我写了下面的代码,但是我认为我的行和列被覆盖了。在import re

import xlwt

f = open("C:\Results\Logging.txt", "r")

book = xlwt.Workbook(encoding="utf-8")

sheet1 = book.add_sheet("New Sheet 1")

sheet2 = book.add_sheet("New Sheet 2")

searchlines = f.readlines()

searchstrings = ['ReallyInteresting =']

timestampline = None

timestamp = None

f.close()

a = 0

tot = 0

row1 = 1

row2 = 1

while a

for i, line in enumerate(searchlines):

for word in searchstrings:

if word in line:

timestampline = searchlines[i-4]

for l in searchlines[i:i+1]: print timestampline,l,

row1 +1

sheet1.write(0, row1, timestampline)

for i in line:

str = timestampline

match = re.search(r'\d{2}:\d{2}:\d{2}.\d{3}', ' ')

if match:

print '\t',match.group(),'\t',searchstrings[a]

row2 +1

sheet2.write(0, row2, searchstrings[a])

print

print

tot = tot+1

break

print 'total count for', '"',searchstrings[a],'"', 'is', tot

tot = 0

a = a+1

book.save("New_Excel.xls")

任何指导都将不胜感激。在

再次感谢,

米克