• pyhon的集合、字典、文件读写应用


    #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(' ').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(' ').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)+' ')



    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--")
  • 相关阅读:
    正则表达式
    Newtonsoft.Json
    MVC之参数验证(三)
    MVC之参数验证(二)
    MVC之参数验证(一)
    MVC之模型绑定
    导致存储过程重新编译的原因
    IFormattable,ICustomFormatter, IFormatProvider接口
    oracle将id串转换为名字串
    oracle查看表空间大小及使用情况
  • 原文地址:https://www.cnblogs.com/zhouj/p/7269278.html
Copyright © 2020-2023  润新知