简体   繁体   English

Python仅打印最后输入的数据

[英]Python only printing last data entered

I have an assignment to create a class that holds the name of an employee, id number, department, and job title. 我有一个作业来创建一个类,其中包含员工的姓名,身份证号,部门和职务。 The user should be able to input the information for multiple employees and have all of the information printed out at the end. 用户应该能够输入多个员工的信息,并在最后打印出所有信息。

The problem I am facing is that only the last employee's information is being printed out. 我面临的问题是只有最后一位雇员的信息才被打印出来。

import pickle
import employee
data = 'data.dat'

def main():
    output_file = open(data, 'wb')
    end_of_file = False

keep_going = 'Y'
while keep_going == 'Y':
    name = str(input('Name of employee: '))
    ID_num = int(input('Employee ID number: '))
    dep = str(input('Department: '))
    job = str(input('Job Title: '))

    emp = employee.Employee(name, ID_num)
    emp.set_department(dep)
    emp.set_job_title(job)
    pickle.dump(emp, output_file)
    keep_going = input('Enter another employee file? (Use Y / N): ')


    input_file = open(data, 'rb')
    while not end_of_file:
        try:
            emp = pickle.load(input_file)
            display_data(emp)
        except EOFError:
            end_of_file = True

    input_file.close()


    if keep_going == 'N':
        print(display_data(emp))
output_file.close()


def display_data(emp):
        print('Name','\t','\t','ID Number','\t','Department','\t','\t','Job Title')
        print(emp.get_name(), '\t', emp.get_ID_num(),'\t','\t',emp.get_department(),'\t','\t',emp.get_job_title())

main()

If anyone knows why this is happening and has any suggestions on how to fix it I would really appreciate it as I am new to python and don't fully understand all of the concepts 如果有人知道为什么会这样,并且对如何解决它有任何建议,我将不胜感激,因为我是python的新手,并且不完全了解所有概念

You need to store the employees in memory and then write to the file in the end. 您需要将员工存储在内存中,然后最后写入文件。 Also, I don't understand why you need this bit of code, it doesn't seem to be doing anything : 另外,我不明白为什么您需要这段代码,它似乎没有做任何事情:

input_file = open(data, 'rb')
while not end_of_file:
    try:
        emp = pickle.load(input_file)
        display_data(emp)
    except EOFError:
        end_of_file = True

input_file.close()

So we remove this, and make some other modifications. 因此,我们将其删除,然后进行其他一些修改。 Your modified code : 您修改的代码:

import pickle
import employee
data = 'data.dat'

def display_data(emp):
        print('Name','\t','\t','ID Number','\t','Department','\t','\t','Job Title')
        print(emp.get_name(), '\t', emp.get_ID_num(),'\t','\t',emp.get_department(),'\t','\t',emp.get_job_title())

def main():
    output_file = open(data, 'wb')

    emp_list = []
    keep_going = 'Y'
    while keep_going == 'Y':
        name = str(input('Name of employee: '))
        ID_num = int(input('Employee ID number: '))
        dep = str(input('Department: '))
        job = str(input('Job Title: '))

        emp = employee.Employee(name, ID_num)
        emp.set_department(dep)
        emp.set_job_title(job)
        emp_list.append(emp)
        keep_going = input('Enter another employee file? (Use Y / N): ')

    pickle.dump(emp_list, output_file)
    output_file.close()

    if keep_going == 'N':
        input_file = open(data, 'rb')
        employees = pickle.load(open(data, "rb"))

        for emp in employees:
            print(display_data(emp))

main()

Also, the printing can be made cleaner : 另外,可以使打印更整洁:

from tabulate import tabulate
def display_data(employees):
    infos = []
    for emp in employees:
        infos.append([emp.get_name(), emp.get_ID_num(), emp.get_department(), emp.get_job_title()])
    print(tabulate(infos, headers=["Name", "ID num", "Department", "Job Title"], tablefmt="fancy_grid"))

So, to print, replace 因此,要打印,更换

for emp in employees:
    print(display_data(emp))

with

display_data(employees)

HTH. HTH。

Every time pickle.dump() is called, it overwrites the existing file. 每次调用pickle.dump()时,它都会覆盖现有文件。 So firstly you need to store all the employees in a list and then write it to file using dump(). 因此,首先需要将所有员工存储在列表中,然后使用dump()将其写入文件。 While retrieving also you need to load data from file using pickle.load() into a list. 检索时,还需要使用pickle.load()将文件中的数据加载到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM