懵懂的菜鸟

Stay hungry,Stay foolish.

导航

b3_trcd_EDCD_new

  1 # -*- coding:utf-8 -*-
  2 import re
  3 
  4 
  5 year="17A"
  6 ss='./data/'
  7 filename = ss+'EDCD%s.txt'%year  
  8 
  9 
 10 '''
 11 适应新版本
 12 '''
 13 
 14 
 15 p1 = r"^\s{4}(?:X|\W)\s{2}(C\d\d\d)\s.+\n"
 16 p2 = r"^(\d\d\d)\s{4}\d{4}\s{2}.+[CM]\s+(?:.*\.\.\d+|.*)\n"#C      n3
 17 p3 = r"^\d\d\d\s{4}(\d{4})\s{2}.+[CM]\s+(?:.*\.\.\d+|.*)\n"
 18 p4 = r"^\d\d\d\s{4}\d{4}\s{2}.+([CM])\s+(?:.*\.\.\d+|.*)\n"
 19 p5 = r"^\d\d\d\s{4}\d{4}\s{2}.+[CM]\s+((?:.*\.\.\d+|.*))\n"
 20 
 21 p6 = r"^(\d\d\d)\s{4}\d{4}\s{2}.+[^\d]$\n"
 22 p7 = r"^\d\d\d\s{4}(\d{4})\s{2}.+[^\d]$\n"
 23 p8 = r"^\s{13}.+([CM])\s+(?:.*\.\.\d+|.*)\n"
 24 p9 = r"^\s{13}.+[CM]\s+((?:.*\.\.\d+|.*))\n"
 25 pattern1 = re.compile(p1)
 26 pattern2 = re.compile(p2)
 27 pattern3 = re.compile(p3)
 28 pattern4 = re.compile(p4)
 29 pattern5 = re.compile(p5)
 30 
 31 pattern6 = re.compile(p6)
 32 pattern7 = re.compile(p7)
 33 pattern8 = re.compile(p8)
 34 pattern9 = re.compile(p9)
 35 fr = open(filename)
 36 temp = "";
 37 flag = 0
 38 for line in fr.readlines():
 39     matcher1 = re.findall(pattern1,line)
 40     matcher2 = re.findall(pattern2,line)
 41     matcher3 = re.findall(pattern3,line)
 42     matcher4 = re.findall(pattern4,line)
 43     matcher5 = re.findall(pattern5,line)
 44 
 45     matcher6 = re.findall(pattern6,line)
 46     matcher7 = re.findall(pattern7,line)
 47     matcher8 = re.findall(pattern8,line)
 48     matcher9 = re.findall(pattern9,line)
 49     #print matcher
 50     w2 = open(ss+'b3_%s.csv'%year,'a')#a代表追加 w代表重写
 51     if matcher1:
 52         flag = 1
 53         for j in matcher1:
 54             temp = j
 55     if ((matcher2!=[])and(flag ==1 or 5 or 9)):
 56         flag = 2
 57         w2.write("\n"+temp+",")
 58         for j in matcher2:
 59             for k in j:
 60                 w2.write(k)
 61 
 62     if ((matcher3!=[])and(flag ==2)):
 63         flag = 3
 64         w2.write(",")
 65         for j in matcher3:
 66             for k in j:
 67                 w2.write(k)
 68 
 69     if ((matcher4!=[])and(flag ==3)):
 70         flag = 4
 71         w2.write(",")
 72         for j in matcher4:
 73             for k in j:
 74                 w2.write(k)
 75         
 76     if ((matcher5!=[])and(flag ==4)):
 77         flag = 5
 78         w2.write(",")
 79         for j in matcher5:
 80             for k in j:
 81                 w2.write(k)
 82 
 83     if ((matcher6!=[])and(flag ==1 or 5 or 9)):
 84         flag = 6
 85         w2.write("\n"+temp+",")
 86         for j in matcher6:
 87             for k in j:
 88                 w2.write(k)
 89     if ((matcher7!=[])and(flag ==6)):
 90         flag = 7
 91         w2.write(",")
 92         for j in matcher7:
 93             for k in j:
 94                 w2.write(k)
 95     if ((matcher8!=[])and(flag ==7)):
 96         flag = 8
 97         w2.write(",")
 98         for j in matcher8:
 99             for k in j:
100                 w2.write(k)
101         
102     if ((matcher9!=[])and(flag ==8)):
103         flag = 9
104         w2.write(",")
105         for j in matcher9:
106             for k in j:
107                 w2.write(k)
108 w2.close( )

 

posted on 2017-08-25 13:43  懵懂的菜鸟  阅读(130)  评论(0编辑  收藏  举报