how can i convert a python list into a file? [closed]

i am doing a python OOP exercise and i want to convert a list into containing variables such as name, age and job into a file, i tried 2 methods but just one worked, this was the one that worked (also the whole code)

class Person:
    def __init__(self, name, job, age):
        self.name = name
        self.job = job
        self.age = age


class PersonManagement:
    def __init__(self):
        self.persons = []

    def add_person(self, name, job, age):
        person = Person(name, job, age)
        self.persons.append(person)

    def persons_file(self):
        with open("persons_file", "w") as file:
            for line in self.persons:
                file.write(f"{line.name} n")

the class method named persons_file is the one i used to make the file, i did that, but, the thing is if i put online write(line) it only writes the memory addres, i solved that by writing the name Person’s name attribute but i want the whole instance written in the file, how can i do that? i want name, age and job there

4

You can convert Person into a dataclass, so it’s easier to work with in your context. By overwriting the __str__ method we can customize how the data will be represented when it is cast to a str. All members of __persons are cast to str and written to the file.

from dataclasses import dataclass, asdict
import json


@dataclass
class Person:
    name:str
    age:int
    job:str
    
    def __str__(self) -> str:
        return ', '.join(map(str, asdict(self).values()))


class PersonManagement:
    DEFAULT_FILE = "persons_file.json"

    def __init__(self, fp:str|None = None):
        # private vars - no external/remote access
        self.__persons = [] 
        self.__fp      = fp or PersonManagement.DEFAULT_FILE
        
    @property 
    def persons(self) -> list: 
        # copy, maintain no modifiable access to `__persons` or it's members
        # indexes reformatted to str
        return [*map(str, self.__persons)]

    # if `save` is a str it will be considered a filepath
    def append(self, *args, save:bool|str=False, **kwargs) -> None:
        self.__persons.append(Person(*args, **kwargs))
        if save: self.save((self.__fp, save)[isinstance(save, str)])

    # format and save `__persons` as comma-separated strings
    def save(self, fp:str|None = None) -> None:
        with open(fp or self.__fp, "w") as file:
            file.write(',n'.join(p for p in self.persons))

Having a generic text file with arbitrary data in it is not very useful. You can switch to the json package to store the information in a way that you can easily reload. Below is a working example of one way you could structure your program to have a complete in/out system. The only real changes we have made here are:

  1. Person is mapped to dict instead of str
  2. json is used to format the dict to JSON
  3. the file is saved as json
  4. a load method is introduced
from dataclasses import dataclass, asdict
import json


@dataclass
class Person:
    name:str
    age:int
    job:str


class PersonManagement:
    DEFAULT_FILEPATH = "persons_file.json"

    def __init__(self, fp:str|None = None):
        # private vars - no external/remote access
        self.__persons = [] 
        self.__fp      = fp or PersonManagement.DEFAULT_FILEPATH
        
    @property 
    def persons(self) -> list: 
        # copy, maintain no modifiable access to `__persons` or it's members
        # indexes reformatted to dict
        return [*map(asdict, self.__persons)]

    # if `save` is a str it will be considered a filepath
    # `save` must be set via keyword
    def append(self, *args, save:bool|str=False, **kwargs) -> None:
        self.__persons.append(Person(*args, **kwargs))
        if save: self.save((self.__fp, save)[isinstance(save, str)])

    # format and save `__persons` as json
    def save(self, fp:str|None = None) -> None:
        with open(fp or self.__fp, "w") as file:
            file.write(json.dumps(self.persons, indent=4, default=str))
    
    # overwrite `__persons` from json        
    def load(self, fp:str|None = None) -> None:
        with open(fp or self.__fp, "r") as file:
            self.__persons = [Person(**data) for data in json.load(file)]

Using JSON for this might be fine, but there are a number of reasons why it isn’t that great. For instance: if you used the shelve package, you could simply store each person directly to the database. You don’t need to load the entire database to get data, and you don’t need to overwrite the entire database to add data. The syntax for working with a shelve database is mostly the same as dict.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật