根据工作天数向工作组分配名称

问题描述

这很难解释,但请与我联系。我目前正在尝试完成在线课程的问题,而我实际上不知道该怎么办。问题的场景使我成为办公室的程序员,需要创建一个程序来分配参加会议的特定员工的程序。任务给了我两个文本文件一个标题的文本文件“ confPack.txt” ,其内容

基本会议包 奖励会议包

还有另一个标题 “ employees.txt” 内容为:

威廉姆斯,马里兰州

阮,荣,Y

金斯利,玛格丽特

Kline,Bob,Y,Y

Mitchell,Frank,Y

Lowe,Elizabeth,Y,Y

基本上,我需要根据他们参加会议的天数,将某些工作人员分配给他们相应的小组/“小组”。 employee.txt文件中的“ Y”表示他们参加的天数(一个Y =出勤一天)。

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

报告日期:[dd / mm / yyyy] *我已经正确显示了时间

与会者:[姓,名]包:[1或2天包],[两天包]

这是到目前为止我的代码

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文件,该文件显示为:

基本会议包

奖金会议包

并为其员工打印出基本或奖励会议包。

    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,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 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,*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())]