将对象数组保存在 python 中的文件中

save array of object in file in python

我想将我的对象数组保存在 python 中的文件中,我测试了 pickle、numpy.. 但它不起作用。 我想在文件中保存它的实例的 class 是:

class pesrson():
def __init__(self,name,centralwidget):
    self.name=name
    self.centralwidget=centralwidget
def config(self):
    self.frame_eployee = QFrame(self.centralwidget)
    self.frame_eployee.setObjectName(u"frame_eployee")
    self.frame_eployee.setGeometry(QRect(20, 590, 321, 51))
    self.frame_eployee.setStyleSheet(u"color:rgb(85, 170, 255)")
    self.frame_eployee.setFrameShape(QFrame.StyledPanel)
    self.frame_eployee.setFrameShadow(QFrame.Raised)
    self.label_fix_logo_user = QLabel(self.frame_eployee)
    self.label_fix_logo_user.setObjectName(u"label_fix_logo_user")
    self.label_fix_logo_user.setGeometry(QRect(10, 0, 41, 41))
    self.label_fix_logo_user.setPixmap(QPixmap(u"D:/V2.0.0/Programme/qt/UI design/picture/user.svg"))
    self.label_name_employee = QLabel(self.frame_eployee)
    self.label_name_employee.setObjectName(u"label_name_employee")
    self.label_name_employee.setGeometry(QRect(60, 10, 101, 21))
    self.label_name_employee.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                           "font: 25 10pt \"Segoe UI Light\";")
    self.label_tim_login = QLabel(self.frame_eployee)
    self.label_tim_login.setObjectName(u"label_tim_login")
    self.label_tim_login.setGeometry(QRect(190, 10, 61, 21))
    self.label_tim_login.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                       "font: 25 10pt \"Segoe UI Light\";")
    self.label_time_logOut = QLabel(self.frame_eployee)
    self.label_time_logOut.setObjectName(u"label_time_logOut")
    self.label_time_logOut.setGeometry(QRect(250, 10, 61, 21))
    self.label_time_logOut.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                         "font: 25 10pt \"Segoe UI Light\";")
    self.label_name_employee.setText(QCoreApplication.translate("MainWindow", self.name, None))
    self.label_tim_login.setText(
        QCoreApplication.translate("MainWindow", u"<html><head/><body><p align=\"center\">08:34</p></body></html>",
                                   None))
    self.label_time_logOut.setText(
        QCoreApplication.translate("MainWindow", u"<html><head/><body><p align=\"center\">08:34</p></body></html>",
                                   None))
    return self.frame_eployee

我用这段代码创建了这个对象:

 p=pesrson("faezeh",self.ui.centralwidget)
    self.ui.verticalLayout.addWidget(p.config())
    self.tree.append(p)

现在我想保存这个数组然后读取它们并且可以使用所有属性。 这个错误是当我使用 json :

TypeError: Object of type person is not JSON serializable
<employee.pesrson object at 0x000001DD17C7B7F0>

当我使用 pickle 时出现这个错误:

Possibly unsupported): cannot pickle 'PySide2.QtWidgets.QFrame' object

您可以使用标准库中的 pickle 模块,也可以使用数据库而不是文件。我认为它可以解决您的问题。