#coding:utf-8
#定义一个全局变量set来临时存放不重复的邮编
s = set()
#定义一个全局变量dic字典类型来存放处理结果,key为邮编,value为邮编出现的次数
dic = {}
#收集不重复的邮编
def collectCode(infile):
with open(infile,'r') as f:
for line in f:
lines = line.strip('\r\n').split(',')
s.add(lines[5])
#初始化键值对,key为set里面的值,value为0
def initDic():
for code in s:
dic[code] = 0
#对不重复的code统计
def countCode(infile):
with open(infile,'r') as f:
for line in f:
adcode = line.strip('\r\n').split(',')[5]
if adcode in dic.keys():
dic[adcode] = dic[adcode] + 1
else:
print("Error:"+adcode)
#把结果写入文件
def storeDic(dic,output,server,date):
with open(output,'w') as f:
for k,v in dic.items():
f.write(date+','+str(server)+','+k+','+str(v)+'\r\n')
if __name__ == "__main__":
print("start---")
server = 1
date = '2017-08-01'
infile = "C:\\Users\\andy\\Desktop\\2017-08-01-0.log"
outfile = "C:\\Users\\andy\\Desktop\\2017-08-01-0_1.txt"
collectCode(infile)
initDic()
countCode(infile)
storeDic(dic,outfile,server,date)
print("over--")