下面是我的代码
read_csv.py
from www.models import Attendance , Employee
import csv
import datetime
dataReader = csv.reader(open('www/attendace.csv'), delimiter=',', quotechar='"')
dic = {}
for row in dataReader:
if row[0] != 'Ac-No': # Ignore the header row, import everything else
account_number = row[0]
stime = row[2]
if account_number not in dic:
dic[account_number] = []
dic[account_number].append(stime)
for acc, stimes in dic.iteritems():
checkin = stimes.pop(0)
print checkin
employee = Employee.objects.get(acc_no= acc)
print str(employee.acc_no)
d = checkin[:-4].strip()
check_in = datetime.datetime.strptime(d, "%m/%d/%Y %H:%M" )
check_out = None
for stime in stimes:
stime = stime[:-4].strip()
stime = datetime.datetime.strptime(stime, "%m/%d/%Y %H:%M" )
if stime.date() == check_in.date():
check_out = stime
print check_out
else:
attend = Attendance()
attend.check_in = check_in
attend.check_out = check_out
attend.employee = employee
attend.save()
check_in = stime
check_out= None
我想拥有每个account_number
,check_in
和check_out
日期,所以我有员工的 dic和(和一起)account_number
的列表。问题是我只得到 1 名员工的输出并且它是错误的,例如,如果我继续运行,emp 1 有 3 条签入和结账记录,员工 1 可以一直到 30 ..有人可以帮忙吗?stime
check_in
check_out