Python only printing last data entered












0















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










share|improve this question























  • In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

    – Deepak Saini
    Nov 28 '18 at 3:59











  • why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

    – muratgu
    Nov 28 '18 at 4:00











  • How do I avoid overwriting the other employees?

    – kawalek26
    Nov 28 '18 at 4:04











  • You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

    – Selcuk
    Nov 28 '18 at 4:11
















0















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










share|improve this question























  • In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

    – Deepak Saini
    Nov 28 '18 at 3:59











  • why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

    – muratgu
    Nov 28 '18 at 4:00











  • How do I avoid overwriting the other employees?

    – kawalek26
    Nov 28 '18 at 4:04











  • You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

    – Selcuk
    Nov 28 '18 at 4:11














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 3:52









kawalek26kawalek26

131




131













  • In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

    – Deepak Saini
    Nov 28 '18 at 3:59











  • why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

    – muratgu
    Nov 28 '18 at 4:00











  • How do I avoid overwriting the other employees?

    – kawalek26
    Nov 28 '18 at 4:04











  • You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

    – Selcuk
    Nov 28 '18 at 4:11



















  • In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

    – Deepak Saini
    Nov 28 '18 at 3:59











  • why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

    – muratgu
    Nov 28 '18 at 4:00











  • How do I avoid overwriting the other employees?

    – kawalek26
    Nov 28 '18 at 4:04











  • You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

    – Selcuk
    Nov 28 '18 at 4:11

















In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

– Deepak Saini
Nov 28 '18 at 3:59





In pickle.dump(emp, output_file) you are overwriting the employees. So at the end, only the last information you entered is available to you.

– Deepak Saini
Nov 28 '18 at 3:59













why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

– muratgu
Nov 28 '18 at 4:00





why don't you accumulate the data in memory (array, list, etc) and just list that in the end? do you have to store it on disk?

– muratgu
Nov 28 '18 at 4:00













How do I avoid overwriting the other employees?

– kawalek26
Nov 28 '18 at 4:04





How do I avoid overwriting the other employees?

– kawalek26
Nov 28 '18 at 4:04













You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

– Selcuk
Nov 28 '18 at 4:11





You can only serialize one object using pickle.dump. Either create different files, or combine all objects in one list as @muratgu suggested.

– Selcuk
Nov 28 '18 at 4:11












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer

































    0














    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.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511853%2fpython-only-printing-last-data-entered%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      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.






      share|improve this answer






























        0














        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.






        share|improve this answer




























          0












          0








          0







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 4:25

























          answered Nov 28 '18 at 4:13









          Deepak SainiDeepak Saini

          1,609816




          1,609816

























              0














              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.






              share|improve this answer




























                0














                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.






                share|improve this answer


























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 4:34









                  rakesh.sahurakesh.sahu

                  100110




                  100110






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511853%2fpython-only-printing-last-data-entered%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks