我有一个小的 Python 脚本需要修改,因为度量文件的格式略有变化。我根本不了解 Python,并试图诚实地自己修复它。这些更改对我来说很有意义,但显然脚本仍然存在一个问题。否则,其他一切都在工作。脚本如下所示:
import sys
import datetime
##########################################################################
now = datetime.datetime.now();
logFile = now.strftime("%Y%m%d")+'.QE-Metric.log';
underlyingParse = True;
strParse = "UNDERLYING_TICK";
if (len(sys.argv) == 2):
if sys.argv[1] == '2':
strParse = "ORDER_SHOOT";
underlyingParse = False;
elif (len(sys.argv) == 3):
logFile = sys.argv[2];
if sys.argv[1] == '2':
strParse = "ORDER_SHOOT";
underlyingParse = False;
else:
print 'Incorrect number of arguments. Usage: <exec> <mode (1) Underlying (2) OrderShoot> <FileName (optional)>'
sys.exit()
##########################################################################
# Read the deployment file
FIput = open(logFile, 'r');
FOput = open('ParsedMetrics.txt', 'w');
##########################################################################
def ParseMetrics( file_lines ):
ii = 0
tokens = [];
for ii in range(len(file_lines)):
line = file_lines[ii].strip()
if (line.find(strParse) != -1):
tokens = line.split(",");
currentTime = float(tokens[2])
if (underlyingParse == True and ii != 0):
newIndex = ii-1
prevLine = file_lines[newIndex].strip()
while (prevLine.find("ORDER_SHOOT") != -1 and newIndex > -1):
newIndex -= 1;
tokens = prevLine.split(",");
currentTime -= float(tokens[2]);
prevLine = file_lines[newIndex].strip();
if currentTime > 0:
FOput.write(str(currentTime) + '\n')
##########################################################################
file_lines = FIput.readlines()
ParseMetrics( file_lines );
print 'Metrics parsed and written to ParsedMetrics.txt'
一切工作正常,除了应该反向迭代前几行以添加自上次发生 UNDERLYING_TICK 事件以来的 ORDER_SHOOT 数字的逻辑(从代码开始:if (underlyingParse == True and ii != 0):.. .) 然后从正在处理的当前 UNDERLYING_TICK 事件行中减去该总数。这是正在解析的文件中的典型行的样子:
08:40:02.039387(+26): UNDERLYING_TICK, 1377, 1499.89
基本上,我只对最后一个数据元素(1499.89)感兴趣,它是以微秒为单位的时间。我知道这一定很愚蠢。我只需要另一双眼睛。谢谢!