用于字符串的Python扫描txt文件然后写入Excel然后继续下一个匹配

时间:2014-04-30 14:08:37

标签: python excel xlwt

首先感谢您阅读本文。我通常在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文件,当我浏览这个长列表时,该文件会被填充。基本上我希望找到“ReallyInteresting”字符串并捕获它的值“0”或“1”,然后将此值发送到xls中的第2列。同时在xls的第1列中,我希望发布与“ReallyInteresting”字符串组的输出关联的时间戳,有点像这样......

Column1         
07:14:03.155        
07:14:04.189
...     

Column2
1
0
....

我编写了以下代码,但我认为我的行和列被覆盖了。

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<len(searchstrings):
    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")

非常感谢任何指导。

再次感谢, MikG

1 个答案:

答案 0 :(得分:0)

问题可能在这里:

row1 +1

它返回(无处)row1的递增值,但row1未受影响。

将其更改为

row1 += 1

同样适用于row2