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