初次在ubuntu下使用python进行简单的统计
昨天花了半天了解了python语言,今天写个程序试一下效果
#-*- coding:utf-8 -*-#统计CDN数量和比例import globimport stringinfile = glob.glob('*.sites')outfile = file('1.result','w')length = len(infile)for i in infile:temp = file(i)cnt = 0while True:line = temp.readline()if len(line) == 0:breakcnt=cnt+1outfile.write(string.ljust(i[:-6],15))outfile.write(string.ljust(str(cnt),15))outfile.write(string.ljust(str(cnt)+'%',15)+'\n')temp.close()outfile.close()#统计各网站使用的CDN数量,详细列表和排序class Record:website = ''number = 0cdn = []def __init__(self,website,number,cdn):self.website = websiteself.number = numberself.cdn = cdninfile2 = file('cntop100')outfile = file('2.result','w')li = []while True:line = infile2.readline()if len(line) == 0:breaktempnumber = 0tempcdn = []for i in infile:tempfile = open(i)while True:templine = tempfile.readline()#print templineif len(templine) == 0:breakif cmp(templine,line) == 0:tempnumber = tempnumber+1tempcdn.append(i[:-6])tempfile.close()record = Record(line,tempnumber,tempcdn)li.append(record)li.sort(lambda x,y: cmp(x.number, y.number),reverse=True)for l in li:outfile.write(string.ljust(l.website[:-1],30))outfile.write(string.ljust(str(l.number),15))if l.number != 0:for c in l.cdn:outfile.write(c)if c != l.cdn[-1]:outfile.write(',')outfile.write('\n')else:outfile.write('--\n')infile2.close()outfile.close()