Last active
December 17, 2015 10:38
-
-
Save thanoojgithub/d6b4a4f022fad7811845 to your computer and use it in GitHub Desktop.
file parsing using pickle in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
class Employee(): | |
'Common base class for all employees' | |
empCount = 0 | |
'11001,sriram,M,married,1989-09-12,30000,tl,d003' | |
def __init__(self, eid, name, gender, mstatus, dob, salary, role, did): | |
self.eid = eid | |
self.name = name | |
self.gender = gender | |
self.mstatus = mstatus | |
self.dob = dob | |
self.salary = salary | |
self.role = role | |
self.did = did | |
Employee.empCount += 1 | |
def displayEmployee(self): | |
print ("eid : ", self.eid,", Name : ", self.name,", gender : ", self.gender,", mstatus : ", self.mstatus, "dob : ", self.dob, ", Salary: ", self.salary, ", role : ", self.role, ", did: ", self.did) | |
"This would create first object of Employee class" | |
emp1 = Employee(11001,"sriram","M","married","1989-09-12",30000,"TL",1003) | |
"This would create second object of Employee class" | |
emp2 = Employee(11002,"seeta","F","married","1988-09-12",25000,"SSE",1004) | |
emp1.displayEmployee() | |
emp2.displayEmployee() | |
print ("Total Employee %d" % Employee.empCount) | |
print ('-------------------------Serialization using pickle-----------------------------') | |
emps = [] | |
fh = open('employees_input.txt','r') | |
elines = fh.readlines() | |
for ei in range(len(elines)): | |
ecols = elines[ei].split(",") | |
emps.insert(ei,Employee(ecols[0],ecols[1],ecols[2],ecols[3],ecols[4],ecols[5],ecols[6],ecols[7])) | |
print (emps[ei].did) | |
print ("eid : ", emps[ei].eid,", name : ", emps[ei].name,", gender : ", emps[ei].gender,", mstatus : ", emps[ei].mstatus, "dob : ", emps[ei].dob, ", salary: ", emps[ei].salary, ", role : ", emps[ei].role, ", did: ", emps[ei].did) | |
fh.close() | |
print ('-------------------------Serialization using pickle-----------------------------') | |
'emptying files' | |
open('employees_output.txt', 'w').close() | |
emp_opFile = open('employees_output.txt', 'w') | |
emp_opFile_wb = open('employees_output_wb.pickle', 'wb') | |
for ei in range(len(emps)): | |
print ("eid : ", emps[ei].eid,", name : ", emps[ei].name,", gender : ", emps[ei].gender,", mstatus : ", emps[ei].mstatus, "dob : ", emps[ei].dob, ", salary: ", emps[ei].salary, ", role : ", emps[ei].role, ", did: ", emps[ei].did) | |
empsLine = emps[ei].eid+", "+emps[ei].name+", "+emps[ei].gender+", "+emps[ei].mstatus+", "+emps[ei].dob+", "+emps[ei].salary+", "+emps[ei].role+", "+emps[ei].did | |
emp_opFile.write(empsLine) | |
pickle.dump(emps, emp_opFile_wb) | |
emp_opFile.close() | |
emp_opFile_wb.close() | |
print ('-------------------------DE-serialization using pickle-----------------------------') | |
deSerEmps = [] | |
temp_pie=0; | |
with open("employees_output_wb.pickle", "rb") as input_file: | |
for pie in pickle.load(input_file): | |
emp = Employee(pie.eid,pie.name,pie.gender,pie.mstatus,pie.dob,pie.salary,pie.role,pie.did) | |
print ("eid : ", emp.eid,", name : ", emp.name,", gender : ", emp.gender,", mstatus : ", emp.mstatus, "dob : ", emp.dob, ", salary: ", emp.salary, ", role : ", emp.role, ", did: ", emp.did) | |
deSerEmps.insert(temp_pie, emp) | |
temp_pie = temp_pie+1 | |
print(deSerEmps) | |
input_file.close() | |
''' | |
employees_input.txt | |
10001,sriram,M,married,1989-09-12,30000,tl,d003 | |
10002,seeta,F,married,1989-09-12,30000,tl,d003 | |
10003,lakshman,M,married,1986-08-28,20000,ml,d004 | |
10004,bharatha,M,married,1986-12-01,19000,ml,d004 | |
10005,sethrugna,M,married,1989-09-12,15000,sse,d003 | |
10006,hanuma,M,single,1989-09-12,18000,sse,d003 | |
10007,ahalya,F,single,1989-09-12,18000,sse,d003 | |
NOTE :: | |
If the file does not exists, open(name,'r+') will fail. | |
You can use open(name, 'w'), which creates the file if the file does not exist, but it will truncate the existing file. | |
Alternatively, you can use open(name, 'a'); this will create the file if the file does not exist, but will not truncate the existing file. | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment