#How to put text file as list in python.

6 messages · Page 1 of 1 (latest)

mental cave
#

I have this list

patients = [Patient('Sara','Smith', 20, '07012345678','B1 234','Cold','',''), Patient('Will','Smith', 24, '07012345678','B1 234','Diabetic','',''),Patient('Sam','Jones', 21, '07012345678','B1 234','Cold','',''), Patient('Mike','Jones', 37,'07555551234','L2 2AB','Diabetic','',''), Patient('Daivd','Smith', 15, '07123456789','C1 ABC','Heart Disease','','')]

I want to put in text file. and then use the text file in other functions. Please help

#

this is the Patient Class:```py
class Patient:
"""Patient class"""

def __init__(self, first_name, surname, age, mobile, postcode, illness, appointment, doctor):
    
    self.first_name = first_name
    self.surname = surname
    self.age = age
    self.mobile = mobile
    self.illness = illness
    self.postcode = postcode 
    self.appointment  = 'Not Set'
    self.doctor = doctor



   
def full_name(self) :
    return self.first_name +" "+ self.surname

def get_appointment(self):
    return self.appointment

def get_doctor(self) :
    return self.doctor

def link(self, doctor):
    """Args: doctor(string): the doctor full name"""
    self.doctor = doctor

def print_symptoms(self):
    """prints all the symptoms"""
    return self.illness
    #ToDo4
def appointment(self):
    return self.appointment

#def as_json(self):
    
    #return {"firstname": self.full_name(), "doctor": self.doctor }

#def from_json(cls, patient_data):
    #return cls(**patient_data)
    


def __str__(self):
    return f'{self.full_name():^18}|{self.doctor}|{self.age:^8}|{self.mobile:^15}|{self.postcode:^10}|{self.illness:^15}|{self.appointment:^1}'
#

The scenario is, able to register and view patients from the text file. ```py
Class Admin:
def patient_management(self, patients)
if op == '1':
print("-----View Patients-----")

        print('ID |      Full Name     |      Doctor`s Full Name      | Age |    Mobile     | Postcode         | Symptoms | Appointment')
        self.view(patients)
elif op == '2

':
# get patient details
print("-----Register Patient-----")
first_name, surname, age, mobile, postcode, illness, appointment = self.get_patient_details()
#ToDo4

        # check if the name is already registered
        name_exists = False
        for patient in patients:
            if first_name == patient.full_name() and surname == patient.full_name:
                print('Name Already Exist')
                name_exists = True
                break
            else:
                name_exists = False
        if name_exists == False:
                    patients.append(Patient(first_name, surname, age, mobile, postcode, illness, appointment,''))
                    print('Patient Registered')
#

and then get rid of the patient list

quick snow
#

You could store it as json in a file

#

But you can't store classes in a json file so you'd need a way to convert them to something like a dict.