【发布时间】:2015-07-16 08:35:14
【问题描述】:
我正在尝试将时间戳从文件中添加到我的搜索结果中。
我的代码是:
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=gridDeltaMarkerIncomingData 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>
我想在 RAW STATS 行中获取那个时间戳,所以它看起来像:
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
【问题讨论】:
-
您正在使用二进制和运算符。这不是您想要的,请改用
and。参见例如bool(1 and 2)和bool(1 & 2). -
我将 '&' 更改为 'and'。给出了同样的结果。它有什么变化? bool(1 and 2) 和 bool(1 & 2) 有什么区别?
-
当然它给出了相同的结果。它碰巧适用于您的示例,但我提供了两个不同的示例。您在这里编程(错误的)C 而不是 Python。在 C 中,出于同样的原因,存在
&&运算符,Python 的等效运算符是and。但这只是对您的实际代码的评论,而不是试图解决您的问题。那必须等待另一个答案/评论。 -
如果您正在寻找时间戳,您必须找到一个标准来找到它们。你能提供更多数据吗?根据您写的内容,我认为我们不可能使用这些时间戳...
-
@adrianus 我认为类似: