标签:
第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。写代码,对每月通话时间做个统计。
刚好用的是联通呢,科科。
Ok,拿到数据了,保存为文件“comu.xls”,现在打算:
1. 统计通话总时长
2. 统计主叫和被叫次数
3. 统计通话总费用
4. pip了matplotlib,试着把数据做成图表(白pip了,py3自带pylab里包含了matplotlib)
嗯,下面开工!
Code:
import xlrd import re from pylab import * def get_xls_data(filename): # 读取xls数据 book = xlrd.open_workbook(filename) sheet = book.sheet_by_index(0) content = {} for i in range(sheet.nrows): content[i+1] = sheet.row_values(i)[1:] #print(content[i+1]) print("Liez的3月话费单:") # 统计话费 cost = 0 for i in range(sheet.nrows-1): cost += float(content[i+2][7]) print("通话费:", round(cost, 2),"元") # 统计被叫主叫次数 call = 0 becall = 0 for i in range(sheet.nrows-1): if(content[i+2][3] == "主叫"): call += 1 else: becall += 1 total = call + becall print("主叫",call,"次,被叫",becall,"次,共计",total,"次通话") # 统计通话时间 time = {} for i in range(sheet.nrows-1): time[i] = content[i+2][1] day = {} for i in range(31): day[i+1] = 0 rday = re.compile(‘-(\d+?) ‘) for i in range(sheet.nrows-1): daycompile = rday.findall(content[i+2][1]) t = int(daycompile[0]) day[t] += 1 daytimes = (list(day.values())) dates = (list(day.keys())) # 统计通话时长 sec = 0 min = 0 for i in range(sheet.nrows-1): rsec = re.compile(‘(\d+)秒‘) rmin = re.compile(‘(\d+)分‘) seci = rsec.findall(content[i+2][2]) mini = rmin.findall(content[i+2][2]) sec += int(seci[0]) if(len(mini)==1): min += int(mini[0]) if(sec >= 60): t = sec / 60 sec = sec % 60 min = min + t print("通话时长:%d分%d秒"%(min,sec)) #三月日通话次数统计图 plt.plot(dates, daytimes) grid(True) title("Call Times Every Day") plt.show() #根据被叫主叫次数作饼状图 figure(2, figsize=(6,6)) fracs = [call/total, becall/total] # 饼状图按被叫和主叫分成两部分的比例 labels = ‘Call‘, ‘Becall‘ pie(fracs, labels=labels, autopct=‘%1.1f%%‘, shadow=True, startangle=90, colors = ("b", "y")) title("Call & Becall") show() get_xls_data("comu.xls")
输出:
Liez的3月话费单:
通话费:15.15 元
主叫 45 次,被叫 64 次,共计 109 次通话
通话时长:193分40秒
两个图表:
ps:感觉这题除了麻烦还是麻烦= - =好在正则表达式用得开始溜起来啦(? •?ω•?)?
标签:
原文地址:http://www.cnblogs.com/liez/p/5410729.html