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

如何解决根据工作天数向工作组分配名称

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-