【问题标题】:Having problems pickling and loading for class在课堂上酸洗和加载时遇到问题
【发布时间】:2021-04-28 05:27:53
【问题描述】:

所以提示是: 阶段 1:员工类

编写一个名为 Employee 的类,该类在属性中保存有关员工的以下数据:姓名、ID 号、部门和职位。

编写完类后,编写一个程序,创建三个Employees对象来保存以下数据:

姓名、身份证号、部门、职务

Susan Meyers 47899 会计副总裁

Mark Jones 39119 IT 程序员

Joy Rodgers 81774 制造工程师

程序应该将此数据存储在三个对象中,然后在屏幕上显示每个员工的数据。

第二阶段:员工管理系统

本练习假设您已经为第 1 阶段创建了 Employee 类。创建一个将 Employee 对象存储在字典中的程序。使用员工 ID 号作为密钥。程序应该提供一个菜单,让用户执行以下操作:

  • 在字典中查找员工

  • 向字典中添加新员工

  • 更改字典中现有员工的姓名、部门和职位

  • 从字典中删除员工

  • 退出程序

当程序结束时,它应该腌制字典并将其保存到文件中。每次程序启动时,它都应该尝试从文件中加载腌制字典。如果文件不存在,程序应该以空字典开始。

所以在大多数情况下,我能够创建类,并且能够创建菜单,但在将员工放入文件并加载以便我可以使用菜单时遇到问题。这是我的代码:

import EmployeeClass
import pickle



def lookup(dictionary):
    id_num = input("What is the employee's ID number")
    if id_num in dictionary.keys():
    print(id_num, ":", dictionary[id_num].str())
else:
    print("I'm sorry we don't have that number in our registry. Try again")
    lookup(dictionary)

def add(dictionary):
    name = input("What is their name?")
    id_num = input("What is their ID number?")
    department = input("What department do they work in?")
    title = input("What position do they hold?")
    entry = EmployeeClass.Employee(name, id_num, department, title)
    dictionary[id_num] = entry
    return dictionary

def change(dictionary):
    id_num = input("Enter the ID Number off the employee you would like to change:")
    if id_num in dictionary.keys():
        tempID = id_num
        newName = input("What would you like to change the name to?")
        newID = input("What would you like to change their ID Number to?")
        newDepartment = input("What department does this person work in?")
        newTitle = input("What title does this person hold?")
        entry = EmployeeClass.Employee(newName, newID, newDepartment, newTitle)
        dictionary[newID] = entry
        del dictionary[tempID]
        print("Employee changed successfully")
    else:
        print("Employee not found. Try again")


    def delete(dictionary):
        id_num = input("Enter the ID Number of the employee that would like to delete")
        if id_num in dictionary.keys():
        del dictionary[id_num]
    else:
        print("That employee was not found")


def save_close(dictionary):
    output_file = open('employee.dat', 'wb')
    pickle.dump(dictionary, output_file)
    output_file.close()


def main():
    employee_data = open("employee.dat", 'wb')
    emp1 = EmployeeClass.Employee("Susan Myers", 47899, "Accounting", "Vice President")
    emp2 = EmployeeClass.Employee("Mark Jones", 39119, "IT", "Programmer")
    emp3 = EmployeeClass.Employee("Joy Rodgers", 81774, "Manufacturing", "Engineer")
    pickle.dump(emp1, employee_data)
    pickle.dump(emp2, employee_data)
    pickle.dump(emp3, employee_data)
    employee_data.close()
    input_file = open('employee.dat', 'rb')
    pickle.load(input_file)
    input_file.close()
    employee_dictionary = {}

     next = True
     while next:
        print("Welcome to the the Employee Management System. Would you like to:\n")
        print("Lookup an employee? Press 1 \n")
        print("Add a new employee? Press 2 \n")
        print("Alter an existing employee? Press 3 \n")
        print("Delete an existing employee's information? Press 4 \n")
        print("Save and Close? Press 5 \n")

        user_choice = input(int())
        menu = {"1": lookup, "2": add, "3": change, "4": delete, "5": save_close}
        x = menu[user_choice](employee_dictionary)

        if user_choice == 2:
            employee_dictionary.update(x)
        if user_choice ==3:
            employee_dictionary.update(x)
        if user_choice ==4:
            employee_dictionary.update(x)

main()

幸运的是,感谢有人回答,我能够解决酸洗问题,但是在加载菜单时,它说我的 Employee 对象没有属性键。感谢您的回复!

【问题讨论】:

  • 请尽量准确地找出您的问题,并尽可能减少必要的开销并编辑您的帖子(人们更有可能做出回应)。目前有太多的上下文需要涉水
  • 对不起,这是我第一次发帖,我应该多考虑一下。

标签: python class dictionary object pickle


【解决方案1】:

你做错了什么是你没有将 depickled 文件分配给任何变量。

您需要更改此代码块

    input_file = open('employee.dat', 'rb')
    pickle.load(input_file)
    input_file.close()
    employee_dictionary = {}

    input_file = open('employee.dat', 'rb')
    employee_dictionary=pickle.load(input_file)
    input_file.close()

只有这样你才能访问腌制字典。

【讨论】:

  • 哦,哇,我觉得自己很笨。所以我能够做到这一点,但是当我运行它之后它给了我一个错误,说 Employee 对象没有属性键。你会推荐什么?
  • 只要确保,该员工是一个 dict 肯定尝试为它打印一个 type() 语句并检查它。您也可以投票赞成答案是否对您有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2015-12-27
  • 2012-05-08
相关资源
最近更新 更多