简体   繁体   English

在 python 中比较基于 emp_id 的两个 class 对象的实例面临困难

[英]Facing difficulty in python comparing instances of two class objects based on emp_id

I am new to python and i am facing issues with comparing instances of class objects taken from csv files.我是 python 的新手,我在比较取自 csv 文件的 class 对象的实例时遇到问题。 I assigned the data of person.csv to class Employee and benefits.csv to class Benefits.我将 person.csv 的数据分配给 class 员工和福利。csv 分配给 ZA2F2ED4F8EBC2CBB61C21A29DC40 福利。 Now, I need to comapre the instances of both the classes ie, (emp_id).现在,我需要比较这两个类的实例,即 (emp_id)。 Here are my two csv files: person.csv:这是我的两个 csv 文件:person.csv:

emp_id sex  age
1      Male  25
2     Female 26
3      Male   28

benefits.csv:好处。csv:

emp_id code type
1       A1  1
1       B20  2
1       A10  1
2       B20  2
3       A10  1
3       C2   2

code:代码:

class Benefits():
  def __init__(self,emp_id,code,codetype):
    super().__init__()
    self.emp_id = emp_id
    self.code = code
    self.codetype = codetype

class Employee():
  def __init__(self,emp_id,sex,age):
    super().__init__()
    self.emp_id = emp_id
    self.sex = sex
    self.age = age
    self.benefits = []

    def __repr__(self): # specifies how to display an Employee
    return "EMP NO:" + str(self.emp_id) + ",SEX:" + str(self.sex) + ",AGE:" + str(self.age) + ",Benefits:" + str(self.benefits)

  def add_benefits(self,ben):
    self.benefits.append(ben)

dir = os.path.dirname(__file__)
file1 = open(os.path.join(dir,"person.csv"), 'r')
next(file1)
directory = os.path.dirname(__file__)
file2 = open(os.path.join(directory, "benefits.csv"), 'r')
next(file2)
  
for line1 in file1:
    vals1 = list(map(lambda s: s.strip(),line1.split(",")))
    E = Employee(vals1[0],vals1[1],vals1[2])
for line2 in file2:
    vals2 = list(map(lambda s: s.strip(),line2.split(",")))
    B = Benefits(vals2[0],vals2[1],vals2[2])

    if vals1[0] == vals2[0]:  #this condition needs to check for all the emp_id's
        E.add_benefits(Benefits(vals2[0],vals2[1],vals2[2]))
print(E)

but i am getting output only for the last emp_id as follows:但我只为最后一个 emp_id 得到 output 如下:

>>>print(E)
EMP NO:3,SEX:Male,AGE:28,Benefits:[A10,C2]

This should be the Required output:这应该是所需的 output:

EMP NO:1,SEX:Male,AGE:25,Benefits:[A1,B20,A10]
EMP NO:2,SEX:Female,AGE:26,Benefits:[B20]
EMP NO:3,SEX:Male,AGE:28,Benefits:[A10,C2]

could anyone help me with the issue.谁能帮我解决这个问题。 thanks in advance.提前致谢。

This appears to be the trouble spot:这似乎是麻烦点:

for line1 in file1:
    vals1 = list(map(lambda s: s.strip(),line1.split(",")))
    E = Employee(vals1[0],vals1[1],vals1[2])

I can see that you're trying to add all 3 lines to your vals1 list, and then print it.我可以看到您正在尝试将所有 3 行添加到您的 vals1 列表中,然后打印它。 The reason only 1 line is showing is because you are redefining both the vals1 and E variables during every iteration .仅显示 1 行的原因是因为您在每次迭代期间都重新定义了 vals1 和 E 变量。 So, instead of adding your data to the list you're actually wiping it out and rewriting it on every pass.因此,您实际上不是将数据添加到列表中,而是在每次传递时都将其清除并重写。

How to fix it:如何修复它:

Create an empty list to store the value of E on every iteration, and print that list at the end of the program, or simply print E at the end of every iteration.创建一个空列表以在每次迭代时存储 E 的值,并在程序结束时打印该列表,或者在每次迭代结束时简单地打印 E。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM