Python Show-Me-the-Code 第 0020题 统计通话时长

第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 –> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。写代码,对每月通话时间做个统计。


思路:

我的手机号是移动的,所以我选的是导出移动手机号某月的详单。打开表格文件发现每行的格式是:

序号 通话地 通话类型 对方号码 开始时间 通话时长 移动话费 长途话费

它的通话类型是包括套餐,主叫,被叫,上网流量之类的,通过这个我们可以知道那些行是属于通话的时间的。它的通话时长记录格式是hh:mm:ss,在这我把该字符串读出来并使用strptime解析它。Python time.strptime() 函数根据指定的格式把一个时间字符串解析为时间元组。语法:time.strptime(string[, format]),其string为时间字符串,format为格式化字符串。逐行扫描表格,把每次的通话时间累加。


代码:

0020.统计通话时长.py

#!/usr/bin/env python
#coding: utf-8
import xlrd
import time

# 存放文件的路径
filepath = ‘/home/bill/Desktop/list(201504).xls‘

def run():
    # 打开表格
    xls = xlrd.open_workbook(filepath)
    sheet = xls.sheet_by_index(0)
    initiative_call = 0
    passtive_call = 0
    # 遍历表格,查到类型为被叫或主叫的行,记录通话时长
    for i in range(3, sheet.nrows):
        rv = sheet.row_values(i)
        if rv[2] == u‘被叫‘:
            struct_time = time.strptime(rv[5], "%H:%M:%S")
            passtive_call += struct_time.tm_hour * 3600 + struct_time.tm_min * 60 + struct_time.tm_sec
        if rv[2] == u‘主叫‘:
            struct_time = time.strptime(rv[5], "%H:%M:%S")
            initiative_call += struct_time.tm_hour * 3600 + struct_time.tm_min * 60 + struct_time.tm_sec
    print ‘主叫时长:%d分钟‘ % (initiative_call/60)
    print ‘被叫时长:%d分钟‘ % (passtive_call/60)
    print ‘总通话时长:%d分钟‘ % ((passtive_call+initiative_call)/60)

if __name__ =="__main__":
    run()


时间: 2024-08-04 15:56:19

Python Show-Me-the-Code 第 0020题 统计通话时长的相关文章

如何在python脚本开发做code review

在软件项目开发中,我们经常提到一个词"code review".code review中文翻译过来就是代码评审或复查,简而言之就是编码完成后由其他人通过阅读代码来检查代码的质量(可编译.可运行.可读.可维护.可复用),这些性质都比较抽象,但是一般都可以通过以下的检查点来实现: 检查代码的命名方式是否符合规范,代码的可读和可维护必须要求所有参与编码的同事使用的命名有统一的规范(注意每个人有自己的代码风格,但是要符合可读性的代码规范): 检查代码的注释,注释一般包括:1.类要有类用途和使用

Python Show-Me-the-Code 第 0004 题 统计单词

第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数. 思路:用正则表达式匹配响应的单词和数字,然后让Counter计算单词的词频,再用most_common方法返回一个按照词频排序的包含该词语和该词语出现的次数的元组的列表. 0004.统计单词.py #!/usr/bin/env python #coding: utf-8 import re from collections import Counter FILESOURCE = '/home/bill/Desktop/test

面试Python工程师,这几道编码题有必要背背,Python面试题No8

第1题:列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25]. map是python高阶用法,字面意义是映射,它的作用就是把一个数据结构映射成另外一种数据结构. map用法比较绕,最好是对基础数据结构很熟悉了再使用,比如列表,字典,序列化这些. map的基本语法如下: map(函数, 序列1, 序列2, ...) Python 2.x 返回列表. Python 3.x 返回迭代器. list = [1,2,3,

python核心编程第4章课后题答案(第二版75页)

4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with a possible expection of the value(which can be changed only if the object is mutable). 4-5str()and repr() repr() is a built-in function while str() wa

[Python unittest] 3-Organizing test code

组织测试代码 前面已经了解到测试的原理和步骤,但只是默认类string的测试,如果是我们自己写的类改怎么测试呢? 如下 class Widget(object): def __init__(self,name,width=50,height=50): self.name = name self.width = width self.height = height def __repr__(self): return "Widget({0})".format(self.name) # 返

python 核心编程第六章课后题自己做的答案

6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了) 1 'Take a string and remove all leading and trailing whitespace' 2 3 def newStrip(str): 4 'delete blanks around a string' 5 _end = len(str) 6 _start = 0 7 8 # de

python运行错误------Non-UTF-8 code

1.安装-----见:https://www.cnblogs.com/weven/p/7252917.html 本文转载于:http://blog.csdn.net/youyuyixiu/article/details/52886692 当python中的代码有中文时,有时会出现下图错误. 解决方法就是在程序的第一行加上 #coding=gbk 1 这样程序就正确啦,如下图. 原文地址:https://www.cnblogs.com/curo0119/p/8428807.html

python核心编程第2章课后题答案(第二版36页)

2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): print i 2-6 Conditionals n =int( raw_input('enter a number:')) if n < 0: print 'negative' elif n > 0: print 'positive' else: print 'zero' 2-7 Loops and

python核心编程第5章课后题答案

5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the area is:', s ** 2., '(units squared)' print 'the volume is:', s ** 3., '(cubic units)'def cirsph(): r = float(raw_input('enter length of radius: ')) p