Python仅打印最后输入的数据

时间:2018-11-28 03:52:31

标签: python

我有一个作业要创建一个类,其中包含雇员的姓名,身份证号,部门和职务。用户应该能够输入多个员工的信息,并在最后打印出所有信息。

我面临的问题是仅打印出最后一位雇员的信息。

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()

如果有人知道为什么会这样并且对如何解决它有任何建议,我将不胜感激,因为我是python的新手,并且还没有完全理解所有概念

2 个答案:

答案 0 :(得分:0)

您需要将员工存储在内存中,然后最后写入文件。另外,我不明白您为什么需要这段代码,它似乎什么也没做:

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()

因此,我们将其删除,然后进行其他一些修改。您修改的代码:

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()

此外,可以使打印更整洁:

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"))

因此,要打印,更换

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

使用

display_data(employees)

HTH。

答案 1 :(得分:0)

每次调用pickle.dump()时,它都会覆盖现有文件。因此,首先需要将所有员工存储在列表中,然后使用dump()将其写入文件。 检索时,还需要使用pickle.load()将文件中的数据加载到列表中。