根据工作天数将工人姓名分配给组

Assigning worker names to groups based on the number of days they've worked

这很难解释,但请告诉我。我目前正在尝试完成一个在线课程的问题,但我真的不知道该怎么做。这个问题的场景让我成为办公室的一名程序员,需要创建一个程序来分配参加会议的特定员工。该任务给了我两个文本文件; 一个标题为 "confPack.txt" 的文本文件内容为

基本会议包 奖金会议包

还有一个标题为 "employees.txt" ,上面写着:

Williams,Mary,Y

Nguyen,Vinh,,Y

Kingsley,Margret

Kline,Bob,Y,Y

Mitchell,Frank,Y

Lowe,Elizabeth,Y,Y

基本上,我需要根据他们参加会议的天数将某些工作人员分配到他们适当的组/“包”。 employee.txt 文件中的“Y”代表他们参加的天数(一个 Y = 一天的出席)。

课程问题本身要我访问 confpack.txt 文件并将记录读入数组,访问 employees.txt 文件并遍历记录(检查文件末尾)和使用逻辑运算符 select 适当的会议与会者。他们说应该这样显示:

Report date: [dd/mm/yyyy] *i've already displayed the time correctly

Attendee: [Surname, first name] Pack/s: [1 or 2 days pack], [both days pack]

到目前为止我的代码是这样的:

import datetime


dTime = datetime.datetime.now()


confFile = open("confPack.txt", "r+")

print("Report Date: "+ dTime.strftime("%d/%m/%Y"))

print(confFile.read())

with open("employees.txt", "r") as f:
    data = f.readlines()

    for line in data:
        words = line.split(",")
        print(words)

confFile.close()

感谢任何帮助。如果你想知道为什么我不能联系我的课程老师寻求帮助,请相信我,他们从不在线。

编辑:关于@Adirio 我希望输出看起来像这样:

报告日期:2020 年 7 月 9 日

Attendee: [Williams, Mary] Pack/s: [Basic Conference Pack]
Attendee: [Nguyen, Vinh] Pack/s: [Basic Conference Pack]
Attendee: [Kingsley, Margret] Pack/s: [N/A]
Attendee: [Kline, Bob] Pack/s: [Bonus Conference Pack]
Attendee: [Mitchell, Frank] Pack/s: [Basic Conference Pack]
Attendee: [Lowe, Elizabeth] Pack/s: [Bonus Conference Pack]

编辑#2:再次感谢@Adirio 的回答。但是,我实际上需要访问 confPack.txt 文件,该文件显示为:

Basic Conference Pack

Bonus Conference Pack

并为其员工打印基本或奖金会议资料包。

    from datetime import datetime


class Employee:
    def __init__(self, surname, name, *args):
        self.name = name.strip()
        self.surname = surname.strip()
        self.days = 0
        for arg in args:
            if arg.strip() == 'Y':
                self.days += 1


now = datetime.now()
print("Report Date: " + now.strftime("%d/%m/%Y"))


#Here i've tried making a .readlines variable to print out the specific conference pack
conf = open("confPack.txt")
all_lines  = conf.readlines()

with open("employees.txt", "r") as f:
    employees = []
    for line in f.readlines():
        if len(line.strip()) != 0:
            employees.append(Employee(*line.split(",")))

for employee in employees:
    print(f'Attendee: [{employee.surname}, {employee.name}]', end=' ')
    if employee.days == 2:
        print("Pack/s: [" + all_lines[2]+"]") 
    elif employee.days == 1:
        print("Pack/s: [" + all_lines[0]+"]")
    else:
        print("Pack/s: [N/A]")

输出:

Report Date: 09/09/2020
Attendee: [Williams, Mary] Pack/s: [Basic conference pack
]                #As you can see, it prints on a new line
Attendee: [Nguyen, Vinh] Pack/s: [Basic conference pack
]
Attendee: [Kingsley, Margret] Pack/s: [N/A]
Attendee: [Kline, Bob] Pack/s: [Bonus conference pack]
Attendee: [Mitchell, Frank] Pack/s: [Basic conference pack
]
Attendee: [Lowe, Elizabeth] Pack/s: [Bonus conference pack]

Process finished with exit code 0

首先,我将稍微清理一下您的原始代码,删除您正在打开和关闭的文件,并为另一个文件使用 with 子句,因为这是一个非常健康的模式。

from datetime import datetime


now = datetime.now()
print("Report Date: " + now.strftime("%d/%m/%Y"))

with open("confPack.txt", "r+") as confFile:
    print(confFile.read())

with open("employees.txt", "r") as f:
    for line in f.readlines():
        words = line.split(",")
        print(words)

现在让我们开始工作吧。我们将创建一个代表每个员工的 class:

class Employee:
    def __init__(self, surname, name, *args):
        self.name = name
        self.surname = surname
        self.days = 0
        for arg in args:
            if arg.strip() == 'Y':
                self.days += 1

__init__ 方法接受从文件中读取的参数(姓氏、名称和 'Y' 的序列)。名字和姓氏直接分配,而其余参数存储在名为 args 的列表中。如果它等于 'Y',我们将遍历此列表,将 1 天添加到天计数器。 .strip() 部分删除了前导和尾随空格,以便我们可以安全地与 'Y' 进行比较。

总之:

from datetime import datetime


class Employee:
    def __init__(self, surname, name, *args):
        self.name = name.strip()
        self.surname = surname.strip()
        self.days = 0
        for arg in args:
            if arg.strip() == 'Y':
                self.days += 1


print("Report Date: " + datetime.now().strftime("%d/%m/%Y"))

with open("confPack.txt", "r+") as f:
    packs = ['N/A']
    for line in f.readlines():
        if len(line.strip()) != 0:
            packs.append(line.strip())

with open("employees.txt", "r") as f:
    employees = []
    for line in f.readlines():
        if len(line.strip()) != 0:
            employees.append(Employee(*line.split(",")))

# Do whatever you need with the employee list
for employee in employees:
    print(f"Attendee: [{employee.surname}, {employee.name}] Pack/s: [{packs[employee.days]}]")

我们还可以通过使用列表理解来缩短打开文件的部分:

with open("confPack.txt", "r+") as f:
    packs = ['N/A'] + [line.strip() for line in f.readlines() if len(line.strip())]

with open("employees.txt", "r") as f:
    employees = [Employee(line.split(",")) for line in f.readlines() if len(line.strip())]