第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。写代码,对每月通话时间做个统计。
#encoding:utf-8
import xlrd
from functools import reduce
def get_call_diration(filename,sheetname):
workbook = xlrd.open_workbook(filename)
sheetbook = workbook.sheet_by_name(sheetname)
call_list = []
for i in range(1,sheetbook.nrows):
# 将每次的通话时间转换为秒添加到列表中
call_list.append(time_convert_second(sheetbook.cell_value(i,3)))
# 对列表求和,获取总的通话时长
time_sum = reduce(lambda x,y:x+y,call_list)
return time_convert_hour(time_sum)
def time_convert_second(time):
if "小时" in time:
return int(time.split("小时")[0])*3600 + int(time.split("小时")[1].split("分")[0])*60+int(time.split("小时")[1].split("分")[1].split("秒")[0])
elif "分" in time:
return int(time.split("分")[0]) * 60 + int(time.split("分")[1].split("秒")[0])
else:
return int(time.split("秒")[0])
def time_convert_hour(time):
if time >= 3600:
return "%s小时%s分%s秒"%(time//3600,time%3600//60,time%3600%60)
elif time >= 60:
return "%s分%s秒"%(time//60,time%60)
else:
return "%s秒"%time
if __name__ == '__main__':
time = get_call_diration("2019年08月语音通信.xls","2019年08月语音通信")
print("您本月通话总时长为:"+time)