Log parsing for cell phone records


So I totally screwed up the code previously, it didnt have any logic to look at only the target phone number so it was just running calculations on the entire log.  Also tossed in some avg calculations for number of calls and time per day for the hell of it.

So I was curious how much time I spend on the phone to certain people, so I decided to write some quick python to figure this out.


import csv
import sys
import datetime
import decimal

today = datetime.date.today().strftime("%d")
total_monthly = 0
count = 0
call_log = sys.argv[1]
target_number = '123-456-7890'
with open(call_log) as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        # this is the old format
        #(timestamp, caller, locale, code, duration, cost) = row

        # new format
        (datestamp, timestamp, caller, direction, duration) = row
        if caller == target_number:
            count += 1
            new_duration = duration.replace(' ', '')[:-2]
            total_monthly = total_monthly + int(new_duration)

# lets calculate total hours/minutes values
total_hours = total_monthly // 60
remaining_minutes = total_monthly - (total_hours * 60)
#print("%s hours, %s minutes" % str(total_hours), remaining_minutes)

print(str(total_hours)+" hours and "+str(remaining_minutes)+" minutes")
print(str(count)+" total calls")
(month, day, year) = datestamp.split('/')
avg_calls_day = count / int(today)
avg_time_day = total_monthly / int(today)

print("avg per day: "+str(round(avg_calls_day,2))+" calls")
print("avg time/day: "+str(round(avg_time_day,2))+" minutes"


This is pretty straight forward, change the value for target_number to whatever number in the logs you want to look for then let it rip like so

./parse.py file.csv