Reading records to binary file

This is the place for queries that don't fit in any of the other categories.

Reading records to binary file

Postby cramsteems » Wed Jan 29, 2014 8:29 pm

Hi I am currently looking at writing and reading from files and I am having trouble reading this record from this binary file.

Basically what I would like to happen is for the record which is completed and saved to a binary file in my first piece of code to be opened in the second piece of code using the iteration to print the correct pieces of information.

The record works outside of the file saving stuff that I need to understand. So it's just getting the second piece of code to read in the record and display the information required!

Also I will paste in my code showing how I think you should read the list from a binary file It would be awesome if you could take a look!

If anyone could instruct me on what I am doing wrong or what I haven't included I would be very grateful!



THANKS!

CODE FOR WRITE TO BINARY:

Code: Select all
import pickle

with open("test10",mode="wb")as my_file:
   
    class employeeinfo():
        def __init__(self):
            self.name = "-"
            self.number = "-"
            self.hours = "-"
            self.payrate = "-"
            self.pay = "-"

    employeerecords = []

    for count in range(0,2):
        records = employeeinfo()
        records.name = input("Employee name: ")
        records.number = input("Emplyee number: ")
        records.hours = float(input("Hours worked: "))
        records.payrate = float(input("Payrate(per hour): "))
        records.pay = ((records.hours)*(records.payrate))
        employeerecords.append(records)

    pickle.dump(employeeinfo,my_file)




CODE TO READ BINARY FILE:

Code: Select all
import pickle
with open (("test10"),mode = "rb") as my_file:
    employeeinfo = (pickle.load(my_file))
   
    for j in range(0,len(employeerecords)):
    print()
        print("Employee: {0}".format(employeerecords[j-1].name))
        print("Number: {0}".format(employeerecords[j-1].number))
        print("Hours worked: {0}".format(employeerecords[j-1].hours))
        print("Payrate: {0}".format(employeerecords[j-1].payrate))
        print("Pay: £{0}".format(employeerecords[j-1].pay))
Last edited by cramsteems on Wed Jan 29, 2014 11:05 pm, edited 1 time in total.
cramsteems
 
Posts: 10
Joined: Thu Sep 19, 2013 5:11 pm

Re: Reading records to binary file

Postby micseydel » Wed Jan 29, 2014 9:44 pm

Why the double post? viewtopic.php?f=6&t=10770&p=13808#p13808

Also, you don't need to scream at us to not accuse you of a do-it-for-me-post if you actually post code. When it happened here it should have been very clear that what you provided was inadequate; you showed no effort and you did literally ask that it be done for you. If you're doing the right thing we'll happily help although it's really annoying when you post about the same thing in multiple threads, USE CAPS WHICH ARE INTERPRETED AS SHOUTING, or act defensive about a topic you should simply have humility about and move on from.

Regarding your question... Saying that you're "having trouble" is not adequate work on your part to help us to help you. You've already been instructed to read this but I feel compelled to do it again because you either haven't read it or have forgotten. I can see the second bit of code you posted has an indentation error. Is that what your question is about? If you've already overcome that problem, please post corrected code (in this, and not another thread, please) or edit your post here.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1271
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 5 guests