Python 查找最后一个唯一标识符并检查数字,然后将时间与当前时间进行比较
@软的 在我的csv文件中,如下图所示,我试图编写一些代码来循环查找最后一个唯一的类(唯一的类基于元素0和元素1合并在一起),在我的示例中,最后一个唯一的类是:Python 查找最后一个唯一标识符并检查数字,然后将时间与当前时间进行比较,python,python-2.7,csv,Python,Python 2.7,Csv,@软的 在我的csv文件中,如下图所示,我试图编写一些代码来循环查找最后一个唯一的类(唯一的类基于元素0和元素1合并在一起),在我的示例中,最后一个唯一的类是:Class02和CD1,这将等于:Class02CD1 然后,它需要为该唯一类的学生查看元素4(在本例中,只有2名学生,如果其中任何一名学生中存在一个数字,则需要从元素2获取时间,并将其与当前时间进行比较,如果当前时间在给定时间后30分钟或更长时间,则应打印单词“迟到” CSV文件 循环遍历唯一元素以查找最后一个类 检查该唯一类中是否存
Class02
和CD1
,这将等于:Class02CD1
然后,它需要为该唯一类的学生查看元素4(在本例中,只有2名学生,如果其中任何一名学生中存在一个数字,则需要从元素2获取时间,并将其与当前时间进行比较,如果当前时间在给定时间后30分钟或更长时间,则应打印单词“迟到”
CSV文件
- 循环遍历唯一元素以查找最后一个类
- 检查该唯一类中是否存在数字
- 如果该数字存在,那么它应该从元素2获取时间
- 如果这个数字存在,并且我们有一个时间,那么我们应该将它与当前时间进行比较
- 如果当前时间为30分钟或在最后一节课的时间之后,则应打印“迟到”,否则不执行任何操作
这是最后时间比较部分的答案
import csv
dict = {}
key = False
with open('test.csv', mode='r') as infile:
reader = csv.reader(infile,)
first = False
for row in reader:
if first:
first = True
continue
key = row[0]+row[1]
if key in dict:
dict[key].append(row)
else:
dict[key] = [row]
print dict
if(key):
for row in dict[key]:
# do time comparison
pass
首先迭代文件并找出最后一行上的类,然后再次迭代文件,现在如果给定行上的类名等于存储的类名,则在该行应用条件并使用
datetime
模块找出时差
from datetime import datetime
import csv
import time
with open('abc1') as f:
reader = csv.reader(f, delimiter=',')
for line in reader:
pass
class_name = ''.join(line[:2]) #save the name on last line
print class_name
f.seek(0) # Rest the file pointer t o the start of the file
for line in reader:
cls_name = ''.join(line[:2])
if cls_name == class_name:
if line[-2]:
current_dtime = datetime.now()
fetched_time = datetime.strptime(line[2], '%H:%M')
fetched_time = datetime(year=current_dtime.year,
month = current_dtime.month,
day = current_dtime.day,
hour = fetched_time.hour,
minute = fetched_time.minute
)
if ((current_dtime - fetched_time).seconds/ 60.0) > 30.0:
print "late"
你能解释一下关于唯一类的部分吗?它只是元素0和元素1在每一行上合并,所以在我的示例中有4个唯一类(数据组)所有独特的类总是被分组在一起?对算法做一些简单的解释会很好。@barry看起来不错,尽管你知道枚举函数,对吗?哦,哇,你得到类名的方式看起来很可爱,我从来没有想到过。但是我对它返回得晚有一个问题,即使它还不晚…看到了吗我的编辑prints@Hyflex尝试使用以下条件:
if(current\u dtime>fetched\u time)和((current\u dtime-fetched\u time).seconds/60.0)>30.0:
这很好,为什么原来的方法不起作用?@Hyflex((current\u dtime-fetched\u time).seconds)
返回两次之间经过的秒数,即使获取的时间>当前的时间
,它也将始终为正,因此添加了和
条件以检查当前的时间
是否大于获取的时间
。
import csv
dict = {}
key = False
with open('test.csv', mode='r') as infile:
reader = csv.reader(infile,)
first = False
for row in reader:
if first:
first = True
continue
key = row[0]+row[1]
if key in dict:
dict[key].append(row)
else:
dict[key] = [row]
print dict
if(key):
for row in dict[key]:
# do time comparison
pass
from datetime import datetime
import csv
import time
with open('abc1') as f:
reader = csv.reader(f, delimiter=',')
for line in reader:
pass
class_name = ''.join(line[:2]) #save the name on last line
print class_name
f.seek(0) # Rest the file pointer t o the start of the file
for line in reader:
cls_name = ''.join(line[:2])
if cls_name == class_name:
if line[-2]:
current_dtime = datetime.now()
fetched_time = datetime.strptime(line[2], '%H:%M')
fetched_time = datetime(year=current_dtime.year,
month = current_dtime.month,
day = current_dtime.day,
hour = fetched_time.hour,
minute = fetched_time.minute
)
if ((current_dtime - fetched_time).seconds/ 60.0) > 30.0:
print "late"