Python readings – Library – Pickle

#!/usr/bin/python
import pickle

fruit = dict(apple=1, berry=3, cherry=10, date=12, elderberry=22)

days = ("Montag", "Dienstag","Mittwoch","Donnerstag",
 "Freitag", "Samstag","Sonntag")

mix = [ fruit, days ]

with open("output.txt","w") as fhandle:
 pickle.dump(mix, fhandle)

with open("output.txt","rb") as fhandle:
 newmix = pickle.load(fhandle)

newfruits, newdays = newmix

print type(mix), type(newmix)
print type(days), type(newdays)
print type(fruit), type(newfruits)

print newdays
print newfruits
<type 'list'> <type 'list'>
<type 'tuple'> <type 'tuple'>
<type 'dict'> <type 'dict'>
('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag')
{'date': 12, 'cherry': 10, 'berry': 3, 'elderberry': 22, 'apple': 1}
(lp0
(dp1
S'date'
p2
I12
sS'cherry'
p3
I10
sS'berry'
p4
I3
sS'elderberry'
p5
I22
sS'apple'
p6
I1
sa(S'Montag'
p7
S'Dienstag'
p8
S'Mittwoch'
p9
S'Donnerstag'
p10
S'Freitag'
p11
S'Samstag'
p12
S'Sonntag'
p13
tp14
a.

Leave a Reply

Your email address will not be published. Required fields are marked *