python中的时间表程序

2024-10-02 02:34:46 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Python类的简介,我必须:

创建一个“时间表”程序,从Project4中读取时钟程序创建的日志文件,并将工作时间添加到工资单数据库文件中。您的程序应该使用用户id作为数据库的主键(每行的第一个数据项),然后列出员工每轮班的日期和持续时间(每行一个班次)。在

我的时钟工作得很好,只是不知道如何让python计算时间。这是时钟程序到文件的输出。在

ClockIN EmpleyeeNumber: 1234566 ClockTime: 08:54:44 ClockDate: 12/07/2016 
ClockOUT EmpleyeeNumber: 1234566 ClockTime: 08:55:20 ClockDate: 12/07/2016
ClockIN EmpleyeeNumber: 1234567 ClockTime: 09:14:50 ClockDate: 12/07/2016 
ClockOUT EmpleyeeNumber: 1234567 ClockTime: 09:15:02 ClockDate: 12/07/2016 

Tags: 文件用户程序id数据库时间时间表工资单
1条回答
网友
1楼 · 发布于 2024-10-02 02:34:46
from datetime import datetime

filename = 'input.txt'
with open(filename, 'r') as file:
    with open('timeSheet.txt', 'a') as f:
        lines = file.readlines()
        datas = []
        for line in lines:
            class_in_out, _, id, _, time, _, date = line.strip().split(" ")
            exist = False
            for data in datas:
                if data['id'] == id:
                    exists_date = False
                    for date_ in data['date']:
                        if date in date_:
                            date_[date][class_in_out] = time
                            exists_date = True
                            break
                    if not exists_date:
                        data['date'].append({date: {class_in_out: time}})
                    exist = True
                    break
            if not exist:
                data = {'id': id, 'date': [{date: {class_in_out: time}}]}
                datas.append(data)

        outputs = []
        for data in datas:
            output = {'id': data['id'], 'date': []}
            values = data['date']
            for value in values:
                for key in value:
                    start = value[key]['ClockIN']
                    stop = value[key]['ClockOUT']
                    date_start = datetime.strptime(start, '%H:%M:%S')
                    date_stop = datetime.strptime(stop, '%H:%M:%S')
                    delta = date_stop - date_start
                    output['date'].append({key: str(delta)})
            outputs.append(output)
        for output in outputs:
            print(output)
            f.write('{id: ' + output['id'])
            f.write(', date: ' + str(output['date']))
            f.write("}\n")
        f.close()

在输入.txt公司名称:

^{pr2}$

在时间表.txt公司名称:

{id: 1234566, date: [{'12/07/2016': '0:00:36'}, {'13/07/2016': '0:00:20'}]}
{id: 1234567, date: [{'12/07/2016': '0:00:12'}, {'13/07/2016': '0:00:15'}]}

相关问题 更多 >

    热门问题