Thursday 15 September 2011

datetime - Python date reading from a csv file -



datetime - Python date reading from a csv file -

i'm trying calculate sla csv file in python 2.7.8. here's illustration of csv file:

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

as can see there 2 different days on csv file , want programme read them , calculate sla daily. here's program:

#v1.1 du programme de calcul du sla import csv import datetime open("/home/maxime/bureau/dev/exports/export2.csv", 'rb') f: #import required modules reader = csv.reader(f, delimiter=';') count=0 #variable of date "number" row in reader: if row[0] !="dispatch date": # don't want include first line of first column date = datetime.datetime.strptime (row [0],"%y-%m-%d %h:%m:%s") #creating datetime object 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 calcul if date right or not count = count+1 #increment date perform sla calcul result = (count/3927.2)*100 #sla calcul print "le fichier date du", date # print "le sla est de :", result, "%" #display sla , date of file

i don't know how utilize correctly "datetime" function in python help me resolve problem.

try read module named "pandas":

import pandas pd def importdict(filename):#creates function read csv #create info frame csv pandas module df=pd.read_csv(filename+'.csv', names=['systemtime', 'var1', 'var2'],sep=';',parse_dates=[0]) #or:, infer_datetime_format=true) filedates=df.t.to_dict().values()#export info frame python dictionary homecoming filedates #return dictionary work outside function if __name__ == '__main__': filedates = importdict('dates') #start function name of file

this function returns dictionary columns , info such in format can work with. named csv "dates" in system. 1 time dict created can print info want or work data.

hope can help you, in problem similar yours week ago.

python datetime csv format

No comments:

Post a Comment