<p>首先遍历文件并找出最后一行上的类,然后再次遍历文件,现在如果给定行上的类名等于存储的类名,则在那里应用条件并使用<code>datetime</code>模块找出时间差。你知道吗</p>
<pre><code>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"
</code></pre>