Howto with examples on 'pickle' module ==> 28/May/2016


In [4]:
"""
An example to store the output without "pickle"
"""

testfile = 'nopickle.txt'

var1 = 1143
var2 = ["AECS", "LAYOUT", "KUNDALAHALLI"]
var3 = 58.30
var4 = ("Bangalore", 560037)

def ezhudhu():
    with open(testfile, 'w+') as f:
        f.write(str(var1))
        f.write(str(var2))
        f.write(str(var3))
        f.write(str(var4))
    
    f.close()
    
    return None
    
def main():
    ezhudhu()
    
if __name__ == '__main__':
    main()

This is the content of nopickle.txt

1143['AECS', 'LAYOUT', 'KUNDALAHALLI']58.3('Bangalore', 560037)


In [6]:
"""
Let us now read the contents of 'nopickle.txt', also check it's type
Does it retain the original type of the variables ?
"""
with open(testfile, 'r') as f:
    print f.readline()
    print(type(f.readline()))


1143['AECS', 'LAYOUT', 'KUNDALAHALLI']58.3('Bangalore', 560037)
<type 'str'>

In [7]:
"""
An example of store the outputin a file using 'pickle'
"""
import pickle

testfile = 'pickle.txt'

var1 = 1143
var2 = ["AECS", "LAYOUT", "KUNDALAHALLI"]
var3 = 58.30
var4 = ("Bangalore", 560037)

def baree():
    with open(testfile, 'w+') as f:
        pickle.dump(var1, f)
        pickle.dump(var2, f)
        pickle.dump(var3, f)
        pickle.dump(var4, f)
            
    f.close()
    
    return None
    
def main():
    baree()
    
if __name__ == '__main__':
    main()
** This is the content of pickle.txt
See the section 'DATA' in the help(pickle) to understand

what's the various markers mean i.e. I, aS, p1, a.F

I1143

.(lp0

S'AECS'

p1

aS'LAYOUT'

p2

aS'KUNDALAHALLI'

p3

a.F58.3

.(S'Bangalore'

p0

I560037

tp1


In [32]:
"""
Let us now read the contents of 'pickle.txt'.
Do the variables retain their types?
"""

def pickout(fileobj):
    print "what's this file object : " 
    print fileobj
    print "it's type : "
    print type(fileobj)
    print "==" * 5 + "==" * 5
    while True:
        pickline = pickle.load(fileobj)
        yield pickline
    
with open('pickle.txt', 'rb') as f:
    for info in pickout(f):
        print info,
        print type(info)


what's this file object : 
<open file 'pickle.txt', mode 'rb' at 0x7fe56dd3cdb0>
it's type : 
<type 'file'>
====================
1143 <type 'int'>
['AECS', 'LAYOUT', 'KUNDALAHALLI'] <type 'list'>
58.3 <type 'float'>
('Bangalore', 560037) <type 'tuple'>
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-32-03b97f456939> in <module>()
     15 
     16 with open('pickle.txt', 'rb') as f:
---> 17     for info in pickout(f):
     18         print info,
     19         print type(info)

<ipython-input-32-03b97f456939> in pickout(fileobj)
     11     print "==" * 5 + "==" * 5
     12     while True:
---> 13         pickline = pickle.load(fileobj)
     14         yield pickline
     15 

/home/ec2-user/anaconda2/lib/python2.7/pickle.pyc in load(file)
   1382 
   1383 def load(file):
-> 1384     return Unpickler(file).load()
   1385 
   1386 def loads(str):

/home/ec2-user/anaconda2/lib/python2.7/pickle.pyc in load(self)
    862             while 1:
    863                 key = read(1)
--> 864                 dispatch[key](self)
    865         except _Stop, stopinst:
    866             return stopinst.value

/home/ec2-user/anaconda2/lib/python2.7/pickle.pyc in load_eof(self)
    884 
    885     def load_eof(self):
--> 886         raise EOFError
    887     dispatch[''] = load_eof
    888 

EOFError: 

Short notes on the above code output

We are passing the filehandle to the 'pickout' function.

While in this function, we check the argument passed to it and it's type.

[Stackoverflow thread on reading file contents (written using pickle) in a loop] (http://stackoverflow.com/questions/18675863/load-data-from-python-pickle-file-in-a-loop)


In [33]:
"""
Example on how to read contents of 'pickle.txt'.
End of file condition handled.
"""

def pickout(fileobj):
    print "what's this file object : " 
    print fileobj
    print "it's type : "
    print type(fileobj)
    print "==" * 5 + "==" * 5
    try:
        while True:
            pickline = pickle.load(fileobj)
            yield pickline
    except EOFError:
        pass
    
with open('pickle.txt', 'rb') as f:
    for info in pickout(f):
        print info,
        print type(info)


what's this file object : 
<open file 'pickle.txt', mode 'rb' at 0x7fe56dd3cc00>
it's type : 
<type 'file'>
====================
1143 <type 'int'>
['AECS', 'LAYOUT', 'KUNDALAHALLI'] <type 'list'>
58.3 <type 'float'>
('Bangalore', 560037) <type 'tuple'>

In [ ]: