##language:zh ''' 简单的想象,快速的完成! ''' ::-- ZoomQuiet [<>] <> = 问题 = * 试验数据的有效性整理: * {{{ ==================================================================== Main Loop: 0.4MM, line: 4000 M ==================================================================== #161: Time : 0 0 0 1 1 3 3 Event : 0x0111 0x4023 0x0111 0x4011 0x0111 0x1020 0x4024 Code : 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 State : 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 #162: Time : 0 0 0 1 1 2 3 Event : 0x0111 0x4023 0x0111 0x4011 0x0111 0x1020 0x4024 Code : 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 State : 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 ... }}} 如此类似的数据文本 attachment::data.txt * 需要整理为标准的数据列表{{{ #looplength code state (在不同长度下, #把所有不为0x0000的code和他相对应的state一一记录下来) 4000 0x0003 0x00E2 4000 0x0004 0x0115 4000 0x0004 0x0115 . }}} = 简单处理 = * 利用Python 多样柔韧的数据结构,先收集数据再按照要求过滤,最后输出 {{{#!python import os,sys # 读入信息为行数据数组 data = open("data.txt","r").readlines() # 我们期望的数据容器 Matrix = {} """以长度为标记,分别对应的收集对应的数据组成为字典 {"长度":[ [Code 数据组..] ,[State 数据组..] ] ... } """ # 假定数据文本都是规范的,就可以利用条件的固定出现次序来操作 loopKey = "" for line in data: if 40>len(line): # 忽略数据太少的行 if "Main Loop:" in line: # 获取长度信息 loopKey = line.split()[-2] print loopKey Matrix[loopKey]=[] continue elif "====="in line: # 忽略修饰行 continue elif "#1" in line: # 忽略标题行 continue elif "Event :" in line: # 忽略事件数据行 continue elif "Code :" in line: # 开始记录数据到目标容器中 puredata = line.split()[2:] #print puredata Matrix[loopKey].append(puredata) elif "State :" in line: # 开始记录数据到目标容器中 puredata = line.split()[2:] #print puredata Matrix[loopKey].append(puredata) else: pass # 现在我们有了嵌套的数据对象,打印检验一下 #print Matrix["4000"] # 依照不同的长度分组,以各组数据的 Code 数据为标准进行检验归整数据 """期待的数据结构类似: {"长度":[ (Code数据,对应的State数据) ...] } 即一个混合数组,元组,的数据字典 """ result = {} for key in Matrix: # 对应的先创建结果的字典项 result[key]=[] # 因为 Code与State是对应的数据,所以可以利用其相同的索引 Code = Matrix[key][0] State = Matrix[key][1] for i in range(0,len(Code)-1): #print Code[i] #print State[i] if "0x0000"==Code[i]: pass else: # 就是这样的利用! result[key].append((Code[i],State[i])) print result # KO! 现在数据过滤明白了,按照意愿组织一下子以便输出,进一步进行图表制作 # 先在屏幕中测试 # 使用一个总输出变量记录在案 output = "" for key in result: for data in result[key]: #print data exp = key exp += " "+data[0] exp += " "+data[1] #print exp output += exp+"\n" exp = "" print output # KO! 最后写到文件中! open("ZqOut.txt","w").write(output) }}} = Another Solution Using Generator = by QiangningHong {{{#!python #! /usr/bin/env python def code_state(input): """Get non-zero codes with corresponding states. Return a generator, yielding a tuple as (looplength, code, state) each time. input: an iterable object containing original data lines. """ for line in input: s = line.split() if not s: continue elif s[0] == 'Main': looplength = s[4] elif s[0] == 'Code': codes = s[2:] elif s[0] == 'State': states = s[2:] for code, state in zip(codes, states): if code != '0x0000': yield looplength, code, state def analysis(infile, outfile): """Read data from infile, write result to outfile. Both infile and outfile are file objects. """ for looplength, code, state in code_state(infile): print >>outfile, looplength, code, state def main(): import sys from optparse import OptionParser parser = OptionParser(usage='%prog [options] infilename outfilename') options, args = parser.parse_args() if len(args) != 2: parser.error('Invalid arguments') if args[0] == '-': inf = sys.stdin else: inf = file(args[0]) if args[1] == '-': outf = sys.stdout else: outf = file(args[1], 'w') analysis(inf, outf) if __name__ == '__main__': main() }}}