0

是)我有的

我有一个 .csv 文件,其中包含给定日期的员工及其轮班列表,如下所示:

Initials,Last,First,ShiftStart,ShiftEnd
BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a

请注意一个人如何可以有多个班次,下一班次的开始时间可能与另一班次的结束时间相同,也可能不同,并且每个员工都由他们的姓名首字母标识为唯一标识符(适合用作关键。)

我想要的是

我想合并这个 .csv 文件,以便每个员工只有一行。如果该人有多个班次,则检查一个班次的结束时间是否与另一个班次的开始时间相同,然后合并这些班次,但如果不是,则添加两个新列 2ndShiftStart 和 2ndShiftEnd 并将该数据放在那里。

结果应如下所示:

Initials,Last,First,ShiftStart,ShiftEnd,2ndShiftStart,2ndShiftEnd
BAB,Smith,Bob,10:00a,3:00p,,
JCJ,Jones,Jill,11:00a,5:00p,,
JIH,Hernandez,Jose,1:00p,4:00p,5:00p,9:00p
DJM,Martin,Dominique,8:00a,11:00a,,

例如,BAB 工作时间为上午 10 点至下午 1 点,然后是下午 1 点至下午 3 点,因此生成的 .csv 将他列为上午 10 点至下午 3 点工作。

4

1 回答 1

1
#!/usr/bin/env python
import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''

db = {}
for line in s.split('\n'):
     Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
     if Initials in db:
         db[Initials][2].append((ShiftStart,ShiftEnd))
     else:
         db[Initials] = (Last,First,[(ShiftStart,ShiftEnd)])
for Initials,v in db.iteritems():
    Last,First,shifts = v
    sys.stdout.write(Initials + ',')
    sys.stdout.write(Last + ',' + First)
    for shift in shifts:
        ShiftStart,ShiftEnd = shift
        sys.stdout.write(',' + ShiftStart + ',' + ShiftEnd)
    sys.stdout.write('\n')

或者,你可以做一个非常面向对象的程序:

import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''

class Shift(object):
    def __init__(self,ShiftStart,ShiftEnd):
        self.ShiftStart,self.ShiftEnd = ShiftStart,ShiftEnd
    def __str__(self):
        return '%s,%s' % (ShiftStart,ShiftEnd)

class Person(object):
    def __eq__(self, p):
        if self.Initials != p.Initials:
            return False
        if p.Last is not None and self.Last != p.Last:
            return False
        if p.First is not None and self.First != p.First:
            return False
        return True
    def __init__(self,Initials,Last,First):
        self.Initials,self.Last,self.First = Initials,Last,First
        self.Shifts = []
    def __str__(self):
        return '%s,%s,%s' % (self.Initials,self.Last,self.First)

def AddShift(people, person, shift):
    try:
        person = people[people.index(person)]
    except ValueError:
        people.append(person)
    person.Shifts.append(shift)

people = []
for line in s.split('\n'):
     Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
     AddShift(people, Person(Initials,Last,First), Shift(ShiftStart,ShiftEnd))

for person in people:
    print '%s,%s' %(person, ','.join(map(str,person.Shifts)))
于 2013-04-04T16:27:02.417 回答