python对excel数据统计_python读取excel数据做分类统计

#-*- coding:utf-8 -*-

importxlrdfrom datetime importtimedeltadefread_excel(file_excel):#读excel并将需要的数据分类放在数组里

infos=[]

info_file=xlrd.open_workbook(file_excel)

info_sheet=info_file.sheets()[0]

row_count=info_sheet.nrowsfor row in range(1,row_count):

time_string=info_sheet.cell(row,3).value

time_s_sp=time_string.split(':')

infos.append(

{'type':info_sheet.cell(row,2).value,'other_cellphone':info_sheet.cell(row,0).value,'timespan':timedelta(seconds=int(time_s_sp[2]),minutes=int(time_s_sp[1]),hours=int(time_s_sp[0])),'gpscity':info_sheet.cell(row,5).value

}

)returninfosdefcount_cell(list_dirs,infotype):#统计总通话及分类统计结果,存在字典里

result_dir={}

time_all=timedelta(seconds=0)for list_dir inlist_dirs:

time_all+=list_dir['timespan']

info_type=list_dir[infotype]if info_type not inresult_dir:

result_dir[info_type]=list_dir['timespan']else:

result_dir[info_type]+=list_dir['timespan']returntime_all,result_dirdefprint_result(result_dir):#打印数据for k,v inresult_dir.items():print k.encode('utf-8'),vif __name__=="__main__":

list_dirs=read_excel('src.xls')

time_all,result_type=count_cell(list_dirs,'type')

result_cell=count_cell(list_dirs,'other_cellphone')

result_gpscity= count_cell(list_dirs, 'gpscity')print '总通话时间:%s' %time_allprint '按照通话类型分类:'print_result(result_type)print '按照号码分类:'print_result(result_cell[1])print '按照归属地分类:'print_result(result_gpscity[1])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值