我正在尝试从文件中向搜索结果添加时间戳。你知道吗
我的代码是:
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
这基本上可以做到:
输出:
如您所见,我分解了
stat_entry
和date_entry
函数;您可能希望更改这些函数,并添加一些更好的条件来检查给定的行是日期还是统计条目。你知道吗你可以用正则表达式来解决它和其他类似的问题。你知道吗
首先你需要找到时间戳
那你可以用
这里我使用Str作为文件中的一行。 然后使用
TimeStamp = match.group()
来获取时间戳现在同样使用正则表达式来查找
将为您提供统计数据的起始索引: 你可以在那之前加上时间戳。你知道吗
here is a guide on regex
and here is for hit and try
相关问题 更多 >
编程相关推荐