Python show-me-the-code question No. 0020 statistics call duration

Source: Internet
Author: User

No. 0020: after landing China Unicom online Business Hall Select the "Self-service"–>"query", and then select the time period you want to query, click the "Query" button, the bottom of the Query results page, click "Export", will generate similar to October 01, 2014 ~2014 October 31 Call details. xls file. Write the code and make a count of the monthly call times.

Ideas:

My mobile phone number is mobile, so I chose to export the mobile phone number of a month's detailed list. Open a table file to find the format of each line:

Serial number of the phone call type the number of the start time of the call long-distance mobile phone calls

The type of call is included in the package, called, called, Internet traffic and the like, through which we can know that those lines belong to the time of the call. It's call time long record format is HH:MM:SS, where I read the string and parse it using Strptime. The Python time.strptime () function resolves a time string to a time tuple based on the specified format. Syntax: Time.strptime (string[, format]), whose string is a time string, format is a formatted string. Scan the table row by line to accumulate each call time.

Code:

0020. Statistics call duration. py

#!/usr/bin/env python#coding: Utf-8ImportXlrdImportTime# The path where the files are storedfilepath ='/home/bill/desktop/list (201504). xls ' def run():    # Open TableXLS = Xlrd.open_workbook (filepath) sheet = Xls.sheet_by_index (0) Initiative_call =0Passtive_call =0    # traverse the table to find a line of the type called or called, recording the length of the call     forIinchRange3, sheet.nrows): RV = sheet.row_values (i)ifrv[2] ==u ' be called ': Struct_time = Time.strptime (rv[5],"%h:%m:%s") Passtive_call + = Struct_time.tm_hour *3600+ Struct_time.tm_min * -+ struct_time.tm_secifrv[2] ==u ' main call ': Struct_time = Time.strptime (rv[5],"%h:%m:%s") Initiative_call + = Struct_time.tm_hour *3600+ Struct_time.tm_min * -+ struct_time.tm_secPrint ' caller length:%d minutes '% (initiative_call/ -)Print ' called Duration:%d minutes '% (passtive_call/ -)Print ' Total call Duration:%d minutes '% ((passtive_call+initiative_call)/ -)if__name__ = ="__main__": Run ()

Python show-me-the-code question No. 0020 statistics call duration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.