天天看點

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")

任何指導都将不勝感激。在

再次感謝,

米克