在实际工作中,遇到这种场景: KPI(关键性能指标)往往是由基础的计数器组成的,比如KPIn=Cx1+Cx2*Cx3-Cx4/Cx5+(KPIx + Cx6)…+Cxn。Cxn来自于不同的数据库表。Cxn本身有几千个,但KPI涉及的,可能就几百个。因此,需要找出所有的KPIn中,所有的不重复Cxn。这里的难点在于,切割规则,不仅仅是判断C开头的,还要看运算符,括号,是否包含组合的KPIx。最后要去重并排序。
好了,python又来了。轮子要造起来。
`#-*- coding: cp936 -*- import os def writeFile(buf): filename = 'after_spliter.txt' fout = open(filename,'w') try: for c in buf: line = c + '\n' fout.write(line) finally: fout.close() def splitCounter(filename): fin = open(filename,'r') try: buf = [] counter = '' for line in fin.readlines(): sen = line.rstrip('\n') for c in sen: if counter == '' and c == 'C': counter = c continue elif len(counter) > 3 and (c == 'C' or c == 'P' or c == '('): buf.append(counter) counter = '' continue elif counter.startswith('C') and c != '+' and c != '-' and c != '*' and c != '/' and c!= ')' : counter = counter + c continue elif counter.startswith('C') and (c == '+' or c == '-' or c == '*' or c == '/' or c == ')'): buf.append(counter) counter = '' continue if len(buf) != 0: counters = list(set(buf)) counters.sort() writeFile(counters) finally: fin.close() if __name__ == '__main__': splitCounter('counter.txt')