Python文件不读取数据,但将重复值放入列表

Python文件不读取数据,但将重复值放入列表,python,python-3.x,list,Python,Python 3.x,List,您好,我正在从类Patients中的文件读取数据,并将内容传递给appointment类方法。 当内容被拆分时,内容[0]具有['130'、'Ali'、'Male'、'22'、'Cough'],因此我将输入这些值并设置Patient类属性。这是在for循环中完成的,所以来自文件读取的所有对象都被添加到Patient类的列表中。但这样做只是重复地添加一行数据。 代码如下: //Text file Data: ID PatientName Gender

您好,我正在从类Patients中的文件读取数据,并将内容传递给appointment类方法。 当内容被拆分时,内容[0]具有['130'、'Ali'、'Male'、'22'、'Cough'],因此我将输入这些值并设置Patient类属性。这是在for循环中完成的,所以来自文件读取的所有对象都被添加到Patient类的列表中。但这样做只是重复地添加一行数据。 代码如下:

 //Text file Data:
          ID    PatientName     Gender      Age Disease
          130   Ali              Male       22  Cough
          132   Annile           Female     23  Corona
          133     sam            Male       24  Fever
我希望患者列表存储130-132-133,但它在列表的所有三个位置仅存储133。不知道对象创建或传递给Patient类是否是一个问题

//病人

    class Patients:

         def __init__(self):
                 self.patient_id = ""
                 self.patient_name = ""
                 self.patient_age = ""
                 self.patient_gender = ""
                 self.patient_disease = ""
      
             def read_patient_file(self):
                     with open('PatientRecord.txt') as f:
                     content = f.readlines()
                     content = [x.strip() for x in content]
                     del content[0] // first row is of column names so removing it
                     return content
//预约.py

          def patientProgram(list_patient):
               Flag = True
               pat1 = Patients()
               while Flag:
               print("\nPress 1. To Create Patient Record ")
               print("Press 2. To View Patient Records ")
               print("Press 6. To Read Patient Record From File ")
               x = int(input("Please input from Menu Displayed Above which Operation to perform:"))
               if x == 1:
                  list_patient.append(AddPatient())

               elif x == 2:
                   print("**********")
                   print("Total Records in Memory for Patients:" + str(len(list_patient)))
                   for pat in list_patient:
                   print(f'patient_id = {pat.patient_id} for object {pat}')
                    
    
                elif x == 6: // this condition gives issue
                   content = []
                   content = pat1.read_patient_file() // content gets 3 rows of data from text file
                   j = 0
                   for i in content:
                       pat_id, name, gender, age, disease = str(content[j]).split()
                       print("FirstID: " + str(j)+ str(pat_id))
                       pat1.set_patient_record(pat_id, name, gender, age, disease)
                       list_patient.append(pat1)
                       j=j+1

                   print("Total Records for Patients in memory : " + str(len(list_patient)))
                   
             

           from Patients import Patients

           if __name__ == "__main__":
               list_patient = []

               Flag = True
               while Flag:
                  print("\nPress 1. For Patient Options ")
                  print("Press 2. For Doctor Options ")
                  print("Press 3. To Exit Program ")
                  x = int(input("Please Select From Menu:"))
                  if x == 1:
                     patientProgram(list_patient)
                 elif x==3:
                     break

    

您只创建了一个
患者
对象。对于文件中的每个记录,修改
pat1
对象并将其附加到
列表\u patient
列表中。所以所有列表元素都是同一个对象

您需要为文件中的每条记录创建一个新对象,而不是在开始时创建一个对象

另外,
read\u patient\u file()
函数应该是一个类方法,因为它不使用任何
self

class Patients:

     def __init__(self):
             self.patient_id = ""
             self.patient_name = ""
             self.patient_age = ""
             self.patient_gender = ""
             self.patient_disease = ""

     @classmethod
     def read_patient_file(cls):
         with open('PatientRecord.txt') as f:
         content = f.readlines()
         content = [x.strip() for x in content]
         del content[0] // first row is of column names so removing it
         return content

def patientProgram(list_patient):
    Flag = True
    while Flag:
        print("\nPress 1. To Create Patient Record ")
        print("Press 2. To View Patient Records ")
        print("Press 6. To Read Patient Record From File ")
        x = int(input("Please input from Menu Displayed Above which Operation to perform:"))
        if x == 1:
            list_patient.append(AddPatient())

        elif x == 2:
            print("**********")
            print("Total Records in Memory for Patients:" + str(len(list_patient)))
            for pat in list_patient:
                print(f'patient_id = {pat.patient_id} for object {pat}')
                
        elif x == 6: // this condition gives issue
            content = []
            content = Patients.read_patient_file() // content gets 3 rows of data from text file
            j = 0
            for i in content:
                pat1 = Patients()
                pat_id, name, gender, age, disease = str(content[j]).split()
                print("FirstID: " + str(j)+ str(pat_id))
                pat1.set_patient_record(pat_id, name, gender, age, disease)
                list_patient.append(pat1)
                j=j+1

            print("Total Records for Patients in memory : " + str(len(list_patient)))

请修复您发布的代码片段中的缩进以匹配实际代码。这对于理解Python至关重要。