简体   繁体   中英

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

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.

Every time pickle.dump() is called, it overwrites the existing file. So firstly you need to store all the employees in a list and then write it to file using dump(). While retrieving also you need to load data from file using pickle.load() into a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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