简单的想象,快速的完成!

::-- ZoomQuiet [2005-06-20 10:09:12]

1. 问题

  • 试验数据的有效性整理:
  • ====================================================================
    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  
    ...
    

    如此类似的数据文本 Upload new attachment "_data.txt"

  • 需要整理为标准的数据列表

    #looplength       code        state (在不同长度下,
    #把所有不为0x0000的code和他相对应的state一一记录下来)
    4000           0x0003      0x00E2
    4000           0x0004      0x0115
    4000           0x0004      0x0115
    .
    
    

2. 简单处理

  • 利用Python 多样柔韧的数据结构,先收集数据再按照要求过滤,最后输出

   1 import os,sys
   2 # 读入信息为行数据数组
   3 data = open("data.txt","r").readlines()
   4 # 我们期望的数据容器
   5 Matrix = {}
   6 """以长度为标记,分别对应的收集对应的数据组成为字典
   7 {"长度":[
   8     [Code 数据组..]
   9     ,[State 数据组..]
  10     ]
  11     ...
  12 }
  13 """
  14 # 假定数据文本都是规范的,就可以利用条件的固定出现次序来操作
  15 loopKey = ""
  16 for line in data:
  17     if 40>len(line):
  18         # 忽略数据太少的行
  19         if "Main Loop:" in line:
  20             # 获取长度信息
  21             loopKey = line.split()[-2]
  22             print loopKey
  23             Matrix[loopKey]=[]
  24         continue
  25     elif "====="in line:
  26         # 忽略修饰行
  27         continue
  28     elif "#1" in line:
  29         # 忽略标题行
  30         continue
  31     elif "Event :" in line:
  32         # 忽略事件数据行
  33         continue
  34     elif "Code  :" in line:
  35         # 开始记录数据到目标容器中
  36         puredata = line.split()[2:]
  37         #print puredata 
  38         Matrix[loopKey].append(puredata)
  39     elif "State  :" in line:
  40         # 开始记录数据到目标容器中
  41         puredata = line.split()[2:]
  42         #print puredata
  43         Matrix[loopKey].append(puredata)
  44     else:
  45         pass
  46 
  47 # 现在我们有了嵌套的数据对象,打印检验一下
  48 #print Matrix["4000"]
  49 # 依照不同的长度分组,以各组数据的 Code 数据为标准进行检验归整数据
  50 """期待的数据结构类似:
  51 {"长度":[
  52     (Code数据,对应的State数据)
  53     ...]
  54     }
  55 即一个混合数组,元组,的数据字典
  56 """
  57 result = {}
  58 for key in Matrix:
  59     # 对应的先创建结果的字典项
  60     result[key]=[]
  61     # 因为 Code与State是对应的数据,所以可以利用其相同的索引
  62     Code = Matrix[key][0]
  63     State = Matrix[key][1]
  64     for i in range(0,len(Code)-1):
  65         #print Code[i]
  66         #print State[i]
  67         if "0x0000"==Code[i]:
  68             pass
  69         else:
  70             # 就是这样的利用!
  71             result[key].append((Code[i],State[i]))
  72 print result
  73 # KO! 现在数据过滤明白了,按照意愿组织一下子以便输出,进一步进行图表制作
  74 # 先在屏幕中测试
  75 # 使用一个总输出变量记录在案
  76 output = ""
  77 for key in result:
  78     for data in result[key]:
  79         #print data
  80         exp = key
  81         exp += "    "+data[0]
  82         exp += "    "+data[1]
  83         #print exp
  84         output += exp+"\n"
  85         exp = ""
  86 
  87 print output
  88 # KO! 最后写到文件中!
  89 open("ZqOut.txt","w").write(output)

3. Another Solution Using Generator

by QiangningHong

   1 #! /usr/bin/env python
   2 
   3 def code_state(input):
   4     """Get non-zero codes with corresponding states.
   5 
   6     Return a generator, yielding a tuple as (looplength, code, state) each
   7     time.
   8 
   9     input: an iterable object containing original data lines.
  10 
  11     """
  12     for line in input:
  13         s = line.split()
  14         if not s:
  15             continue
  16         elif s[0] == 'Main':
  17             looplength = s[4]
  18         elif s[0] == 'Code':
  19             codes = s[2:]
  20         elif s[0] == 'State':
  21             states = s[2:]
  22             for code, state in zip(codes, states):
  23                 if code != '0x0000':
  24                     yield looplength, code, state
  25 
  26 
  27 def analysis(infile, outfile):
  28     """Read data from infile, write result to outfile.
  29     Both infile and outfile are file objects.
  30     """
  31     for looplength, code, state in code_state(infile):
  32         print >>outfile, looplength, code, state
  33 
  34 
  35 def main():
  36     import sys
  37     from optparse import OptionParser
  38     parser = OptionParser(usage='%prog [options] infilename outfilename')
  39     options, args = parser.parse_args()
  40     if len(args) != 2:
  41         parser.error('Invalid arguments')
  42 
  43     if args[0] == '-':
  44         inf = sys.stdin
  45     else:
  46         inf = file(args[0])
  47     if args[1] == '-':
  48         outf = sys.stdout
  49     else:
  50         outf = file(args[1], 'w')
  51     analysis(inf, outf)
  52 
  53 
  54 if __name__ == '__main__':
  55     main()