主要工作:
1.对从网上营业厅拿到的用户数据.xls文件,通过Python的xlrd进行解析,计算用户的主叫被叫次数,通话时间,通话时段。
2.使用matplotlib画图包,将分析的结果直观的绘制出来。
具体步骤:
1.分析须要的内容
excel文件中包含很多信息,我们分析的时候须要用到的包括,通话起始时间、通话时长、呼叫类型,号码。
使用xlrd模块,读取excel中的数据,用列表的形式保存下来。
1 #coding=utf-8 2 import xlrd 3 4 def readData(url): 5 data=xlrd.open_workbook(url) 6 table=data.sheets()[0] #多张sheet的情况读取第一张 7 nrows=table.nrows 8 ncols=table.ncols 9 list=[] 10 for rownum in range(0,nrows): 11 row=table.row_values(rownum) 12 for i in range(0,ncols): #转码unicode转utf-8 13 row[i]=row[i].encode('utf-8') 14 if row: 15 list.append(row) 16 return list
2.设计用户数据分析类
考虑分析用户的哪些数据,做成一个什么样的效果
1 #coding=utf-8 2 # 用户通话信息类记录 3 class UserDataInfo(object): 4 def __init__(self): 5 self.calling_long = 0 #主叫时长 6 self.called_long = 0 #被叫时长 7 self.call_long = 0 #总时长 8 9 self.calling_times = 0 #主叫次数 10 self.called_times = 0 #被叫次数 11 self.call_times = 0 #总次数 12 13 self.time_intervel=[] #通话时段 14 for i in range(0,12): 15 self.time_intervel.append(0) 16 17 self.day_intervel=[] #每日通话次数 18 for i in range(0,31): 19 self.day_intervel.append(0) 20 21 self.call_freq= None #通话最频繁
3.对时间日期字符串解码编码
实用split函数对字符串分割,将x时x分x秒的格式解码成 x秒的数据存储下来
1 #解码时间 2 def timeDecode(timeStr): 3 hour = min = sec = 0 4 if timeStr.find('小时') != -1: 5 hour = timeStr.split('小时')[0] 6 timeStr = timeStr.split('小时')[1] 7 if timeStr.find('分') != -1: 8 min = timeStr.split('分')[0] 9 timeStr = timeStr.split('分')[1] 10 if timeStr.find('