Python CSV查找平均所需时间

时间:2017-10-03 03:50:43

标签: python csv date time average

我想找到平均时间。价值' x'将允许我得到每一行所用的时间,但我怎样才能找到所有行的平均时间。我会认为这是x除以数量之类的东西,但是我无法找到解决方案...那里的任何专业人士可以帮助我吗?

import datetime,time,csv
from itertools import islice
from Tkinter import Tk      #python GUI programming
from tkFileDialog import askopenfilename
from collections import Counter
from datetime import datetime
import pandas


Tk().withdraw()
category_list=[]
description_list=[]
reported_date=[]
acknowledged_date=[]
count = 0

# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
                   '%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y  %H:%M:%S %p']
#file = askopenfilename() #ask user which file to open
#f = open(file,'r')
with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
    reader = csv.reader(f)
    header = next(reader) #read 2nd line onwards
    data= []  #make a list called data
    for row in reader: #assign data in every column and name them respectively


      for format in date_formats:
        try:

            reported_on = datetime.strptime(row[0], format) #try and get the dates
            acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
            x= acknowledged_on-reported_on #time taken to acknowledge

            #acknowledged_date.append(acknowledged_on)
            #reported_date.append(reported_on)
            count += 1

            break # if correct format, dont test any other formats

        except ValueError:
            pass # if incorrect format, try other formats`enter code here`

1 个答案:

答案 0 :(得分:0)

减去两个datetime个对象会创建一个timedelta个对象。您需要保留总时间,因此请创建一个timedelta对象,并为每个x添加一个对象。

最后,您可以将total_time除以count

import csv
from itertools import islice
from datetime import datetime, timedelta


count = 0
total_time = timedelta()

# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
                '%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y  %H:%M:%S %p']

with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
    reader = csv.reader(f)
    header = next(reader) #read 2nd line onwards

    for row in reader:
        for format in date_formats:
            try:
                reported_on = datetime.strptime(row[0], format) #try and get the dates
                acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
                x = acknowledged_on - reported_on #time taken to acknowledge
                total_time += x
                count += 1
                break # if correct format, don't test any other formats

            except ValueError:
                pass # if incorrect format, try other formats`enter code here`

print "Total time taken:", total_time                
print "Average time taken:", total_time / count

注意:date_formats的逻辑意味着单行中的两个日期将始终共享相同的日期格式。