我實際上試圖從python 2.7.8中的csv文件中計算SLA。 這裏是我的csv文件的例子:從csv文件中讀取Python日期
2014-09-24 23:57:43;0000B169;20
2014-09-24 23:58:05;00012223;20
2014-09-24 23:58:49;00012200;20
2014-09-24 23:59:33;0000B0EA;21
2014-09-25 00:00:17;000121FF;21
2014-09-25 00:00:39;00012217;21
2014-09-25 00:01:01;00012176;20
2014-09-25 00:01:23;00012175;20
正如你可以看到有兩個不同的日子是我的CSV文件,我想我的程序讀取它們和日常計算SLA。 這裏是我的程序:
#V1.1 du programme de Calcul du SLA
import csv
import datetime
with open("/home/maxime/Bureau/Dev/Exports/export2.csv", 'rb') as f: #import the required modules
reader = csv.reader(f, delimiter=';')
count=0 #variable of the date "number"
for row in reader:
if row[0] !="Dispatch date": # we don't want to include the first line of the first column
date = datetime.datetime.strptime (row [0],"%Y-%m-%d %H:%M:%S") #creating the datetime object with the string agrument
if date < datetime.datetime.strptime ("2014-09-26 00:00:00", "%Y-%m-%d %H:%M:%S")and date > datetime.datetime.strptime ("2014-09-25 00:00:00", "%Y-%m-%d %H:%M:%S"): #loop to calcul if the date is correct or not
count = count+1 #increment the date to perform the SLA calcul
result = (count/3927.2)*100 #SLA calcul
print "Le fichier date du", date #
print "Le SLA est de :", result, "%" #Display the SLA and the date of the file
我不知道如何正確使用Python中的「日期時間」功能,使你能幫助我解決我的問題。
什麼是輸出的程序?任何錯誤消息? – greole 2014-10-20 08:40:52
例如,您不需要使用'datetime.datetime.strptime()'創建常量日期值。'datetime.datetime(2014,9,26)'就可以。 – 2014-10-20 08:46:08
但是,您未能告訴我們您的實際*問題*。有錯誤嗎?意外的輸出?你期望什麼產出? – 2014-10-20 08:47:52