查找fi中特定行之后包含时间戳的第一行

2024-05-23 22:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从文件中向搜索结果添加时间戳。你知道吗

我的代码是:

def findIcommingStats():
    #read the result file
    replication_file = open("result.log", "r")

    #create a new temp file for all the prints we will find
    tempFile = open("incomingTemp.txt", "w")

    #loop over the file and move all relevant lines to another temp file
    for line in replication_file:
            if ((line.find('STATISTICS') >= 0) & ( line.find('DeltaMarkerIncomingData') > 0 ) & ( line.find('Counter') == -1  ) &
                     ( line.find('0.00e+00') == -1 ) & ( line.find('0.00') == -1 ) & ( line.find('description') == -1 ) ):
                            tempFile.write(line)
    #cleanup
    replication_file.close()
    tempFile.close()

这将提供我在文件中搜索的字符串,如下所示: “统计信息:name=gridDeltaMarkeringData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445)0)0 8582秒窗口:速率:3.53e-06 MB/秒”

时间戳在那之前大约有20-30行。 我怎样才能在字符串之前把它们打印成行?你知道吗

时间戳看起来像“2015/07/08 10:08:00.079”

文件看起来像:

2015/07/08 10:14:46.971 - #2 - 4080/4064 - AccumulatorManager: ProcessID= RAW STATS:

<statistics>

STATISTICS: name=gridDeltaMarkerIncomingData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445) 0) 0 924 sec window: Rate: 0.00e+00 MB/sec
STATISTICS: name=gridDeltaMarkerIncomingData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445) 0) 0 8582 sec window: Rate: 3.53e-06 MB/sec
STATISTICS: name=gridDeltaMarkerIncomingData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445) 0) 0 63612 sec window: Rate: 4.23e-06 MB/sec

<more statistics>

我想在原始统计行中得到时间戳,所以它看起来像:

2015/07/08 10:14:46.971 STATISTICS: name=gridDeltaMarkerIncomingData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445) 0) 0 924 sec window: Rate: 0.00e+00 MB/sec

2015/07/08 10:14:46.971 STATISTICS: name=gridDeltaMarkerIncomingData kVolSlot=0 GroupCopy(26764 SiteUID(0x3d1d0445) 0) 0 8582 sec window: Rate: 3.53e-06 MB/sec

Tags: thenamerateline时间mbsecfind
2条回答

这基本上可以做到:

def stat_entry(line):
    return line.startswith('STATISTICS')

def date_entry(line):
    return line.startswith('20')

def findIcommingStats():
    date = ''
    with open("result.log", "r") as replication_file:
        with open("incomingTemp.txt", "w") as tempFile:
            for line in replication_file:
                if date_entry(line):
                    date = ' '.join(line.split(' ')[:2]) # set new date
                elif stat_entry(line):
                    tempFile.write(date  + ' ' + line) # write to tempfile

findIcommingStats()

输出:

2015/07/08 10:14:46.971 STATISTICS: name=gridDeltaMarkerIncomingData...
2015/07/08 10:14:46.971 STATISTICS: name=gridDeltaMarkerIncomingData...
2015/07/08 10:14:46.971 STATISTICS: name=gridDeltaMarkerIncomingData...

如您所见,我分解了stat_entrydate_entry函数;您可能希望更改这些函数,并添加一些更好的条件来检查给定的行是日期还是统计条目。你知道吗

你可以用正则表达式来解决它和其他类似的问题。你知道吗

首先你需要找到时间戳

 regexTimeStamp = re.complie('\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}.\d{3}')

那你可以用

match = regexTimeStamp.match(Str)

这里我使用Str作为文件中的一行。 然后使用TimeStamp = match.group()来获取时间戳

现在同样使用正则表达式来查找

regexStat = re.compile('STATISTICS:')

match1 = regexStat.match(str)
match1.start()

将为您提供统计数据的起始索引: 你可以在那之前加上时间戳。你知道吗

here is a guide on regex

and here is for hit and try

相关问题 更多 >