从 csv 文件中读取 Python 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26461422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python date reading from a csv file
提问by Abolah
I'm actually trying to calculate a SLA from a csv file in python 2.7.8. here's an example of my csv file:
我实际上是在尝试从 python 2.7.8 中的 csv 文件计算 SLA。这是我的 csv 文件的示例:
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 you can see there are two different days on my CSV file and I want my program to read them and calculate the SLA daily. here's my program:
如您所见,我的 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
I don't know how to use correctly the "datetime" function in python so could you help me to resolve my problem.
我不知道如何正确使用 python 中的“日期时间”函数,所以你能帮我解决我的问题吗?
采纳答案by Carlos Corral Carre?o
Try to read with the module named "pandas":
尝试使用名为“pandas”的模块阅读:
import pandas as pd
def importdict(filename):#creates a function to read the csv
#create data frame from csv with 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 the data frame to a python dictionary
return fileDATES #return the dictionary to work with it outside the function
if __name__ == '__main__':
fileDATES = importdict('dates') #start the function with the name of the file
This function returns a dictionary with all the columns and the data such in a format you can work with. I named your csv "dates" in my system. Once the dict is created you can print the info that you want or work with the data.
此函数以您可以使用的格式返回包含所有列和数据的字典。我在我的系统中将您的 csv 命名为“日期”。创建 dict 后,您可以打印所需的信息或使用数据。
Hope this can help you, I was in a problem similar to yours a week ago.
希望这可以帮助您,一周前我遇到了与您类似的问题。
回答by Abolah
I found the Solution of my problem so i'm posting it here.
我找到了我的问题的解决方案,所以我把它贴在这里。
#V1.2 du calcul de SLA
#Cette version est opérationnelle
import csv #
from datetime import datetime #import the librairies
from collections import defaultdict #
with open('/home/maxime/Bureau/Dev/Exports/export2.csv', 'rb') as fil:
values = defaultdict(int) #create a dict
reader = csv.DictReader(fil, delimiter=';') #read the csv file
for row in reader:
date = datetime.strptime(row['Dispatch date'], '%Y-%m-%d %H:%M:%S') #datetime value in the right date format
values[date.strftime('%Y-%m-%d')] += 1 #increment the date with a step of 1
for date, value in sorted(values.items()):
result = (value/ 3927.2) * 100 #Sla calcul with the theoritic number of line
print 'Le fichier date du %s' % date #SLA display
print 'Le SLA est de : %d%%' % result
回答by jfs
If the data for the same day is grouped in the file then you could use itertools.groupby(), to compute SLA:
如果同一天的数据分组在文件中,那么您可以使用itertools.groupby(), 来计算 SLA:
#!/usr/bin/env python
import sys
from datetime import datetime
from itertools import groupby
for day, group in groupby(sys.stdin, key=lambda line: line.partition(' ')[0]):
try:
date = datetime.strptime(day, '%Y-%m-%d')
except ValueError:
pass # ignore
else:
count = sum(1 for _ in group)
print('Le fichier date du {date:%Y-%m-%d}'.format(date=date))
print('Le SLA est de {result:.2%}'.format(result=count / 3927.2))
Example:
例子:
$ python compute-daily-sla.py < export2.csv

