1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import re import os import time import codecs def find\_liwu(): filename = os.listdir('logfile') filepath = [] for element in filename: if len(element.split('.txt')) >= 2: filepath.append(element) feiji = '飞机' banka = '办卡' i = 0 bankadic = {} feijidic = {} g = 0 num = [0] patternbanka = unicode('感谢 ','utf8',errors='ignore')+u'[\u4e00-\u9fa5\d\w]+'+unicode(' 送出的 ','utf8',errors='ignore')+unicode(banka,'utf8',errors='ignore')+'[x][\d]+' patternfeiji = unicode('感谢 ', 'utf8', errors='ignore') + u'[\u4e00-\u9fa5\d\w]+' + unicode(' 送出的 ', 'utf8',errors='ignore') + unicode(feiji, 'utf8', errors='ignore') + '[x][\d]+' while i < len(filepath): path = 'logfile/' + filepath[i] with open(path) as f: loglines = f.read().splitlines() j = 0
while j < len(loglines): line = unicode(loglines[j], "utf8", errors="ignore") answer = re.findall(patternbanka,line) answer1 = re.findall(patternfeiji,line) if answer: g += 1 prev = int(num[0]) patternName = u'(?P<name>[\u4e00-\u9fa5\d\w]+)'+unicode(' 送出的 ','utf8',errors='ignore') name = re.findall(patternName,answer[0]) patternNum = unicode(banka,'utf8',errors='ignore')+'[x](?P<num>[\d]+)' num = re.findall(patternNum,answer[0]) num1 = int(num[0]) if name[0] in bankadic.keys(): if num1 == 1: bankadic[name[0]] = bankadic[name[0]] + 1 elif num1 == 10 and prev != 9: bankadic[name[0]] = bankadic[name[0]] + 10 elif num1 == 66 and prev != 65: bankadic[name[0]] = bankadic[name[0]] + 66 elif num1 == 366 and prev != 365: bankadic[name[0]] = bankadic[name[0]] + 366 elif num1 == 520 and prev != 519: bankadic[name[0]] = bankadic[name[0]] + 520 elif num1 == 1314 and prev != 1313: bankadic[name[0]] = bankadic[name[0]] + 1314 else: bankadic[name[0]] = bankadic[name[0]] + 1 else: bankadic[name[0]] = num1 if answer1: g += 1 prev = int(num[0]) patternName = u'(?P<name>[\u4e00-\u9fa5\d\w]+)'+unicode(' 送出的 ','utf8',errors='ignore') name = re.findall(patternName,answer1[0]) patternNum = unicode(feiji,'utf8',errors='ignore')+'[x](?P<num>[\d]+)' num = re.findall(patternNum,answer1[0]) num1 = int(num[0]) if name[0] in feijidic.keys(): if num1 == 1: feijidic[name[0]] = feijidic[name[0]] + 1 elif num1 == 10 and prev != 9: feijidic[name[0]] = feijidic[name[0]] + 10 elif num1 == 66 and prev != 65: feijidic[name[0]] = feijidic[name[0]] + 66 elif num1 == 366 and prev != 365: feijidic[name[0]] = feijidic[name[0]] + 366 elif num1 == 520 and prev != 519: feijidic[name[0]] = feijidic[name[0]] + 520 elif num1 == 1314 and prev != 1313: feijidic[name[0]] = feijidic[name[0]] + 1314 else: feijidic[name[0]] = feijidic[name[0]] + 1 else: feijidic[name[0]] = num1 j+= 1 i += 1
f = codecs.open('liwu.txt','w','utf-8')
i = 0 while i < (len(bankadic)): if bankadic.keys()[i] in feijidic.keys(): f.write(bankadic.keys()[i] + ' '+ str(bankadic[bankadic.keys()[i]])+unicode(' 办卡 ','utf8',errors='ignore')+ str(feijidic[bankadic.keys()[i]])+ unicode(' 飞机','utf8',errors='ignore') +unicode(' 价值 ','utf8',errors='ignore')+ str(bankadic[bankadic.keys()[i]]*6+feijidic[bankadic.keys()[i]]*100)+ ' ''\n') else: f.write(bankadic.keys()[i] + ' '+ str(bankadic[bankadic.keys()[i]])+unicode(' 办卡','utf8',errors='ignore')+ unicode(' 0 飞机','utf8',errors='ignore')+unicode(' 价值 ','utf8',errors='ignore')+ str(bankadic[bankadic.keys()[i]]*6) +'\n') i += 1 i = 0 while i < (len(feijidic)): if feijidic.keys()[i] in bankadic.keys(): f.write(feijidic.keys()[i] + ' '+ str(bankadic[feijidic.keys()[i]])+ unicode(' 办卡 ','utf8',errors='ignore')+ str(feijidic[feijidic.keys()[i]])+unicode(' 飞机 ','utf8',errors='ignore') +unicode(' 价值 ','utf8',errors='ignore')+ str(bankadic[feijidic.keys()[i]]*6+feijidic[feijidic.keys()[i]]*100)+'\n') else: f.write(feijidic.keys()[i] + ' '+ unicode(' 0 办卡 ','utf8',errors='ignore')+ str(feijidic[feijidic.keys()[i]])+unicode(' 飞机','utf8',errors='ignore') +unicode(' 价值 ','utf8',errors='ignore')+ str(feijidic[feijidic.keys()[i]]*100) +'\n') i += 1 if __name__ == "__main__": start_time = time.time() a = find_liwu()
print("--- %s seconds ---" % (time.time() - start_time)) filename = os.listdir('logfile')
|